php无限分类

<?php
class category_model extends CI_Model{
    public $cishop;
    
    public function __construct(){
        parent::__construct();
        $this->cishop = $this->load->database('cishop',true);
    }
        /*
        *$pid
        *retrun array 节点所有后代节点
        */
    
    
    public function get_data($table='ci_category',$where=array()){
         
            if(!empty($where)){
                $res = $this->cishop->get_where($table,$where)->result_array();
            }else{
               $res = $this->cishop->get_where($table,$where)->result_array();
            }
            return $res;
      
    }
    
    public function update_data( $where=array(), $data, $table='ci_category'){
        
        
          $bool = $this->cishop->where($where)->update('ci_category',$data);
          
          
           echo $this->cishop->last_query();
            if($bool){
                return true;
            }else{
                return false;
            }
    }
    
    /**
    *@param $arr 分类信息
    *@param $pid 父id
    *@return $arr 三维数组,前台分类数据
    *
    */
    public function front_cate(){
        $data = $this->query_data('ci_category');
        //var_dump($data);
        return $this->cate_front_list($data);
        
    }
    
    public function cate_front_list($arr , $pid=0){
        
        $child =$this->child($arr,$pid);
        //var_dump($child);
        if(empty($child)){
            return null;
        }else{
            foreach($child as $k=>$v){
                
                    $current_child = $this->cate_front_list($arr, $v['cat_id']);
                    if(empty($current_child)){
                        
                    }else{
                        //该分类节点下面还有子分类节点,则将子节点作为该节点元素保存
                        $child[$k]['child'] = $current_child;
                        
                        //var_dump($child);
                        
                    }
                
                
            }
            
        }
        
        
        
        return $child;
        
    }
    
    public function child($arr, $pid=0){
        $child= array();
        foreach($arr as $v){
            if($v['parent_id'] == $pid){
                //$v['child'] = $this->cate_front_list($arr,$v['cat_id']);
                
                $child[]=$v;
            }
        }
        return $child;
    }
    
    
    
    
    public function query_data($table=''){
         
            if(!empty($where)){
                $res = $this->cishop->get($table)->result_array();
            }else{
               $res = $this->cishop->get($table)->result_array();
            }
            return $res;
      
    }
    
    
    public function list_cat($pid = 0){
        
        $arr = $this->query_data('category');
        
        //$pid=$arr[0]['parent_id'];
        $cat_arr = $this-> _tree($arr,$pid);
        return $cat_arr;
        
    }    
    /*@access public
    
    *@param $arr 要遍历的数组
    *@param $id
    *@param $level 层级
    *
    */
    public function _tree($arr, $id=0, $level=0){
        //保存重组结果
        
        static $tree = array();
        foreach($arr as $v){
            if($v['parent_id'] == $id){
                $v['level'] = $level;
                $tree[] = $v;
                $cid = $v['cat_id'];
                $this->_tree($arr,$cid,$level+1);
            }
        }
        return $tree;
    }
    
    public function insert_cat(){
        $parent_id = $this->input->post('parent_id');
        $measure_unit = $this->input->post('measure_unit');
        $sort_order = $this->input->post('sort_order');
        $is_show = $this->input->post('is_show');
        $show_in_nav = $this->input->post('show_in_nav');
        $cat_recommend = $this->input->post('cat_recommend');
        $cat_desc = $this->input->post('cat_desc');
        $cat_name = $this->input->post('cat_name');
        
        $data = array(
            "cat_name" => $cat_name,
            "parent_id" => $parent_id,
            "cat_desc" => $cat_desc,
            "sort_order" => $sort_order,
            "unit" => $measure_unit,
            "is_show" => $is_show
            
            
            
        );
        
        var_dump($data);

        
        $bool = $this->cishop->insert("ci_category", $data);
        return $bool;
    }
    
}
?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值