1. 程式人生 > >tp3後臺欄目的增刪改查

tp3後臺欄目的增刪改查

模型

<?php
namespace Admin\Model;
use Think\Model;
class CategoryModel extends Model {

   protected $_validate = array(
      array('cate_name','require','欄目名稱必填!',1,regex,3), //預設情況下用正則進行驗證
   );
    
   public function catetree(){
      $data=$this->select();


      return $this->resort($data);
   
   }
   
   public function resort($data,$parentid=0,$level=0){
      static $ret=array();
      foreach($data as $k=>$v){
         if($v['parentid']==$parentid){
            $v['level']=$level;
            $ret[]=$v;
            $this->resort($data,$v['cate_id'],$level+1);
         }
      }
      
      return $ret;
   
   }

}

 

控制器

public function add(){
   $cate=D('category');
   if(IS_POST){
      $data['cate_name']=I('cate_name');
      $data['cate_ename']=I('cate_ename');
      $data['parentid']=I('parentid');

      if($cate->create($data)){
               if($cate->add()){
                   $this->success('欄目新增成功',U('lst'));
               }else{
                   $this->error('欄目新增失敗!');
               }
      }else{
         $this->error($cate->getError());
      }
      
   
      return;
   }

   $cateres=$cate->catetree();
       $this->assign('cateres',$cateres);
       $this->display();
   }

檢視

<form method="post" action="" enctype="multipart/form-data">
   <table class="tbl" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#a8c7ce" onmouseover="changeto()"  onmouseout="changeback()">

     <tr>
       <td height="20" bgcolor="#FFFFFF" class="STYLE6" align="right">欄目名稱</td>
       <td height="20" bgcolor="#FFFFFF" class="STYLE19" align="left"><input name="cate_name" type="text" /></td>
</tr>
   
   <tr>
       <td height="20" bgcolor="#FFFFFF" class="STYLE6" align="right">英文名稱</td>
       <td height="20" bgcolor="#FFFFFF" class="STYLE19" align="left"><input name="cate_ename" type="text" /></td>
</tr>

   <tr>
       <td height="20" bgcolor="#FFFFFF" class="STYLE6" align="right">上級欄目</td>
       <td class="cd" height="20" bgcolor="#FFFFFF" class="STYLE19" align="left">
       <select name="parentid">
       <option value="0">頂級欄目</option>
       <volist name="cateres" id="vo">
           <option value="{$vo.cate_id}"><?php echo str_repeat('-',$vo[level]*8);?>{$vo.cate_name}</option>
       </volist>
       </select>
       
       </td>
</tr>

   <tr>
       <td height="20" colspan="2" bgcolor="#FFFFFF" class="STYLE6" align="center"><input type="submit" value="確定新增" /></td>
       
</tr>
   </table>
   </form>

 

控制器

public function del($cate_id){
   $cate=D('category');
   if($cate->delete($cate_id)){
      $this->success('刪除欄目成功!',U('lst'));
   }else{
      $this->error('刪除欄目失敗!');
   }

}

檢視

<a href="__CONTROLLER__/del/cate_id/{$vo.cate_id}" onclick="return confirm('您確定要刪除該欄目嗎?');">刪除</a>

 

控制器

public function edit($cate_id){
   $cate=D('category');
   
   if(IS_POST){
      $data['cate_id']=I('cate_id');
      $data['cate_name']=I('cate_name');
      $data['cate_ename']=I('cate_ename');

      $data['parentid']=I('parentid');

      if($cate->create($data)){
      if($cate->save()){
         $this->success('欄目修改成功',U('lst'));
      }else{
         $this->error('欄目修改失敗!');
      }
      }else{
         $this->error($cate->getError());
      }
      
   
      return;
   }
   
   $cateresa=$cate->find($cate_id);
   $this->assign('cateresa',$cateresa);


   $cateres=$cate->catetree();
   $this->assign('cateres',$cateres);
       $this->display();
   }

檢視

<form method="post" action="" enctype="multipart/form-data">
   <input type="hidden" name="cate_id" value="{$cateresa.cate_id}" />
   <table class="tbl" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#a8c7ce" onmouseover="changeto()"  onmouseout="changeback()">

     <tr>
       <td height="20" bgcolor="#FFFFFF" class="STYLE6" align="right">欄目名稱</td>
       <td height="20" bgcolor="#FFFFFF" class="STYLE19" align="left"><input name="cate_name" type="text" value="{$cateresa.cate_name}" /></td>
</tr>
   
   <tr>
       <td height="20" bgcolor="#FFFFFF" class="STYLE6" align="right">英文名稱</td>
       <td height="20" bgcolor="#FFFFFF" class="STYLE19" align="left"><input name="cate_ename" type="text" value="{$cateresa.cate_ename}" /></td>
</tr>
   
    <tr>
       <td height="20" bgcolor="#FFFFFF" class="STYLE6" align="right">上級欄目</td>
       <td class="cd" height="20" bgcolor="#FFFFFF" class="STYLE19" align="left">
       <select name="parentid">
       <option value="0">頂級欄目</option>
       <volist name="cateres" id="vo">
       <option <if condition="$cateresa[cate_id] eq $vo[cate_id]">style="display:none;"</if>  <if condition="$cateresa[parentid] eq $vo[cate_id]"> selected="selected"</if>  value="{$vo.cate_id}"><?php echo str_repeat('-',$vo[level]*8);?>{$vo.cate_name}</option>
       </volist>
       </select>
       
       </td>
</tr>

   <tr>
       <td height="20" colspan="2" bgcolor="#FFFFFF" class="STYLE6" align="center"><input type="submit" value="確定修改" /></td>
       
</tr>
   </table>
   </form>

控制器

public function lst(){
   $cate=D('category');
   $cateres=$cate->catetree();
   $this->assign('cateres',$cateres);
       $this->display();
   }

檢視

<volist name="cateres" id="vo">
  <tr>
    <td height="20" bgcolor="#FFFFFF"><div align="center">
      <input type="checkbox" class="selall" name="bdel[]" value="{$vo.cate_id}" />
    </div></td>
    <td height="20" bgcolor="#FFFFFF" class="STYLE6"><div align="center"><span class="STYLE19">{$vo.cate_id}</span></div></td>
    <td height="20" bgcolor="#FFFFFF" class="STYLE19" align="left" style="padding-left:5px;"><?php echo str_repeat('-',$vo[level]*8);?>{$vo.cate_name}</td>

    <td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">{$vo.parentid}</div></td>
    <td height="20" bgcolor="#FFFFFF"><div align="center" class="STYLE21"><a href="__CONTROLLER__/edit/cate_id/{$vo.cate_id}">修改</a> | <a href="__CONTROLLER__/del/cate_id/{$vo.cate_id}" onclick="return confirm('您確定要刪除該欄目嗎?');">刪除</a></div></td>
  </tr>
  </volist>