欧美日韩一区二区三区四区不卡,日韩欧美视频一区二区三区四区,久久精品欧美一区二区三区不卡,国产精品久久久乱弄

咨詢電話:186 7916 6165 咨詢電話:186 7916 6165 (微信同號(hào))    在線QQ:181796286
NEWS BLOG ·
學(xué)無(wú)止境
關(guān)注開優(yōu)網(wǎng)絡(luò) 關(guān)注前沿
PHP Web開發(fā)之權(quán)限模塊
PHP Web開發(fā)之上傳圖片

PHP Web開發(fā)之無(wú)限級(jí)分類

發(fā)表日期:2015-10-26    文章編輯:南昌開優(yōu)網(wǎng)絡(luò)    瀏覽次數(shù):4906    標(biāo)簽:PHP應(yīng)用

ThinkPHP中的無(wú)限級(jí)分類
模版頁(yè)category.html
<script type="text/javascript" src="__PUBLIC__/js/jquery.js"></script>
<script type="text/javascript">
function addclick() {
frm.action = "__URL__/add";//添加事件
}
function editclick() {
frm.action = "__URL__/edit";//修改事件
}
function delclick() {
frm.action = "__URL__/del";//刪除事件
}
 
function isSelected(pid){
var caname=$.trim(pid.options[pid.selectedIndex].text);//獲取下拉列表選中的文本去掉前后空格
document.getElementById("txtmodcaname").value = caname;
document.getElementById("txtdelcaname").value = caname;
document.getElementById("hfid").value = pid.options[pid.selectedIndex].value; 
}
</script>
</head>
<body>
<form id="frm" name="frm" method="post" action="">
    <select onchange="isSelected(this);" name="pid" size="20" id="pid">
      <option>分類列表</option>
      <volist name="alist" id="vo">
        <option value="<{$vo['id']}>">
        <php>        
       for($i=0;$i<$vo['count'];$i++){
       echo "&nbsp;";
       }
        </php>       
        <{$vo['caname']}></option>
      </volist>
    </select>
    <input type="text" name="txtaddcaname" id="txtaddcaname" /> 
    <input name="hfid" type="hidden" id="hfid" />
    <input name="btnAdd" type="submit" onclick="addclick();" id="btnAdd" value="添加" /> 
  <input type="text" name="txtmodcaname" id="txtmodcaname" /> 
  <input type="submit" onclick="editclick();" name="btnMod" id="btnMod" value="修改" /> 
    <input type="text" name="txtdelcaname" id="txtdelcaname" />
    <input type="submit" onclick="delclick();" name="btnDel" id="btnDel" value="刪除" />
</form>

控制器
<?php
header ( "Content-Type: text/html;charset=utf-8" );
class CatAction extends Action {
//頁(yè)面加載
public function index() {
$cat = new Model ( 'category' );
//list=$cat->query("select * from shop_category order by code asc");
$list = $cat->order('code asc')->select ();
foreach($list as $key=>$value){//key為下標(biāo),value為值
$list[$key]['count']=count(explode('0',$value['code']));//explode為分割
}
$this->assign ( 'alist', $list );//分配變量
$this->display ( './Public/category.html' );
}
//添加
public function add() {
$cat = new CatModel ( 'category' );
$caname = trim ( $_POST ['txtaddcaname'] );
if (strlen ( $caname ) == 0) {
echo "<script>alert('請(qǐng)輸入分類名稱');history.back();</script>";
exit ();
}
$pid=isset($_POST['pid'])?(int)$_POST['pid']:0;//獲取pid值
$code = $cat->GenCode ( $pid );//通過(guò)傳入的pid生成code
$data ['caname'] = $caname;
$data ['pid'] = $pid;
$data ['code'] = $code;
$list = $cat->add ( $data );
if ($list > 0) {
echo "<script>alert('添加成功');history.back();</script>";
exit ();
} else {
echo "<script>alert('添加失敗');history.back();</script>";
exit ();
}
}
//修改
public function edit() {
$cat = new Model ( 'category' );
$caname=trim($_POST['txtmodcaname']);
if(strlen($caname)==0){
echo "<script>alert('請(qǐng)輸入分類名稱');history.back();</script>";
exit ();
}
$id=$_POST['hfid'];
$data['caname']=$caname;
$list=$cat->where("id=$id")->save($data);
if($list>0){
echo "<script>alert('修改成功');history.back();</script>";
exit ();
}else{
echo "<script>alert('修改失敗');history.back();</script>";
exit ();
}
}
//刪除
public function del() {
$cat = new Model ( 'category' );
$caname=trim($_POST['txtdelcaname']);
if(strlen($caname)==0){
echo "<script>alert('請(qǐng)選擇分類名稱');history.back();</script>";
exit ();
}
$id=$_POST['hfid'];
$list=$cat->where("id=$id")->delete();
if($list>0){
echo "<script>alert('刪除成功');history.back();</script>";
exit ();
}else{
echo "<script>alert('刪除失敗');history.back();</script>";
exit ();
}
}
}
?>

模型
<?php
header ( "Content-Type: text/html;charset=utf-8" );
class CatModel extends Model{
public function GenCode($pid){
$list=$this->where("pid=$pid")->field("max(code)as bh")-> find();
//$list=$this->query("select max(code) from shop_category where pid=$pid");
if($list["bh"]==""||$list["bh"]==null){
if($pid=="0"){
return "01";
}else{
$tmp1=$this->where("id=$pid")->field('code')-> find();
//$tmp=$this->query("select code from shop_category where id=$pid");
return $tmp1['code']."01";
}
}else{
$tmp2=$this->where("pid=$pid")->field("max(code)as bh")-> find();
//$tmp=$this->query("select max(code) from shop_category where pid=$pid");
$left=substr($tmp2['bh'],0,strlen($tmp2['bh'])-1);
$right=substr($tmp2['bh'],strlen($tmp2['bh'])-1)+1;
return $left.$right;
}
}
}
?>
主站蜘蛛池模板: 宜都市| 沂水县| 汤阴县| 开江县| 双桥区| 平顶山市| 广东省| 长宁区| 罗城| 海淀区| 平果县| 陆河县| 增城市| 平罗县| 交城县| 镇江市| 韶山市| 巴楚县| 梓潼县| 徐水县| 仲巴县| 灵山县| 韩城市| 长岛县| 吉木萨尔县| 上林县| 方山县| 铜陵市| 房产| 嘉兴市| 宜宾市| 南阳市| 镇原县| 景德镇市| 通城县| 南昌县| 德惠市| 区。| 安庆市| 澜沧| 阿拉善左旗|