1. 程式人生 > >問答項目---欄目增刪改方法示例

問答項目---欄目增刪改方法示例

1.5 bsp 方法 -h this spa etop line ucc

具體方法:

// 遞歸重新排序無限極子分類數組
function recursive($array,$pid=0,$level=0){
    $arr = array();
    foreach ($array as $v) {
        if($v[‘pid‘] == $pid){
            $v[‘level‘] = $level;
            $v[‘html‘] = str_repeat(‘--‘,$level);
            $arr[] = $v;            
            $arr = array_merge($arr
,recursive($array,$v[‘id‘],$level+1)); } } return $arr; } //遞歸獲取所有的子分類的ID function get_all_child($array,$id){ $arr = array(); foreach($array as $v){ if($v[‘pid‘] == $id){ $arr[] = $v[‘id‘]; $arr = array_merge($arr,get_all_child($array,$v[‘id‘])); }; };
return $arr; }

增刪改方法示例:

<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends Controller {
    public function index(){
        $cateTopList = M(‘category‘)->select();
        $cateTopList = recursive($cateTopList);
        $this->assign(‘cateTopList‘,$cateTopList
); $this->display(); } public function addTop(){ $this->display(); } public function addCate(){ $data = I(‘post.‘); if(M(‘category‘)->add($data)){ $this->success(‘添加成功‘, ‘index‘); }else{ $this->error(‘添加失敗‘); }; } // 添加子分類 public function addChild(){ $pid = I(‘get.pid‘); $where = array(‘id‘ => $pid); $pCateName = M(‘category‘)->where($where)->getField(‘name‘); $this->assign(‘pid‘,$pid); $this->assign(‘pCateName‘,$pCateName); $this->display(); } //修改分類 public function edit(){ $pid = I(‘get.pid‘); $where = array(‘id‘ => $pid); $pCateName = M(‘category‘)->where($where)->getField(‘name‘); $this->assign(‘pid‘,$pid); $this->assign(‘pCateName‘,$pCateName); $this->display(); } public function editCate(){ $post = I(‘post.‘); $date = array( ‘id‘ => $post[‘pid‘], ‘name‘ => $post[‘name‘] ); if(M(‘category‘)->save($date)){ $this->success(‘修改成功‘,‘Index‘); }else{ $this->error(‘修改失敗‘); }; } //刪除分類 public function delCate(){ $get = I(‘get.pid‘); $cateId = M(‘category‘)->field(array(‘id‘,‘pid‘))->select(); $delId = get_all_child($cateId,$get); $delId[] = $get; $where = array( ‘id‘ => array(‘IN‘,$delId) ); if(!M(‘category‘)->where($where)->delete()){ $this->error(‘刪除失敗‘); }; $this->success(‘刪除成功‘,U(‘index‘)); } }

問答項目---欄目增刪改方法示例