如何显示类别,子类别和子子类别 - codeigniter

时间:2015-09-06 06:01:22

标签: php codeigniter hierarchical-data

我的分类表设计如下

enter image description here

我有三个级别的类别

  1. parent
  2. 子孩子
  3. 如何以下列方式显示它们?

    enter image description here

    (SELECT * FROM categories):我得到以下数组

    Array
    (
        [0] => stdClass Object
            (
                [category_id] => 1
                [category_slug] => 
                [category_glyphicon] => live glypicon
                [category_name] => Live
                [is_parent] => 0
                [is_child] => 0
                [is_sub_child] => 0
                [rf_flag] => 0
                [status] => 1
                [category_description] => 
                [created_on] => 2015-09-04 10:47:54
                [updated_on] => 2015-09-05 22:48:01
            )
    
        [1] => stdClass Object
            (
                [category_id] => 2
                [category_slug] => 
                [category_glyphicon] => dddd
                [category_name] => work
                [is_parent] => 0
                [is_child] => 0
                [is_sub_child] => 0
                [rf_flag] => 0
                [status] => 1
                [category_description] => 
                [created_on] => 2015-09-04 11:41:18
                [updated_on] => 2015-09-04 11:11:18
            )
    
        [2] => stdClass Object
            (
                [category_id] => 3
                [category_slug] => 
                [category_glyphicon] => mmm
                [category_name] => enjoy
                [is_parent] => 0
                [is_child] => 0
                [is_sub_child] => 0
                [rf_flag] => 0
                [status] => 1
                [category_description] => 
                [created_on] => 2015-09-04 11:41:52
                [updated_on] => 2015-09-04 11:12:51
            )
    
        [3] => stdClass Object
            (
                [category_id] => 4
                [category_slug] => 
                [category_glyphicon] => mmm
                [category_name] => for sale
                [is_parent] => 1
                [is_child] => 0
                [is_sub_child] => 0
                [rf_flag] => 0
                [status] => 1
                [category_description] => 
                [created_on] => 2015-09-04 11:42:59
                [updated_on] => 2015-09-04 11:12:59
            )
    
        [4] => stdClass Object
            (
                [category_id] => 5
                [category_slug] => 
                [category_glyphicon] => ccccc
                [category_name] => for rent
                [is_parent] => 1
                [is_child] => 0
                [is_sub_child] => 0
                [rf_flag] => 0
                [status] => 1
                [category_description] => 
                [created_on] => 2015-09-05 12:13:48
                [updated_on] => 2015-09-04 11:43:48
            )
    
        [5] => stdClass Object
            (
                [category_id] => 6
                [category_slug] => 
                [category_glyphicon] => sss
                [category_name] => villas
                [is_parent] => 4
                [is_child] => 1
                [is_sub_child] => 0
                [rf_flag] => 0
                [status] => 1
                [category_description] => 
                [created_on] => 2015-09-05 12:14:57
                [updated_on] => 2015-09-04 11:44:57
            )
    
    )
    

3 个答案:

答案 0 :(得分:0)

尝试以下代码
我以为你有mysqli db连接

 //assuming db connction with mysqli

 $res=$db->query("select * from categories");
 if($res){
   echo "<table>";
   echo "<tr>";
     echo "<td>A</td><td>B</td><td>C</td><td>D</td>";
   echo "</tr>";
   $index = 1;
   while( $row = $res->fetch_array()){

       echo "<td>".$index."</td>";        

       if($row['is_parent']>0){
          echo "<td>".$row['category_name']."</td>";        
          echo "<td>0</td>";        
          echo "<td>0</td>";        
       }else if($row['is_child']>0){
          echo "<td>0</td>";      
          echo "<td>".$row['category_name']."</td>";          
          echo "<td>0</td>";        
       }else if($row['is_sub_child']>0){
          echo "<td>0</td>";        
          echo "<td>0</td>";        
          echo "<td>".$row['category_name']."</td>";        
       }  

        $index++;
    }
    echo "</table>";
   }else{
      echo $db->error;
   }

答案 1 :(得分:0)

我得到了输出

public function get_dt_data()
        {
            error_reporting(1);     
            // data is loading into $list
            $list = $this->category1_model->get_datatables();
            $parenrarray=array();

            foreach($list as $k){
                $parenrarray[$k->category_id]=$k->category_name;
            }

            //echo '<pre>';print_r($list);echo'</pre>';;
            //echo '<pre>';print_r($parenrarray);echo'</pre>';exit();


            $data = array();
            $no = $_POST['start'];
            foreach ($list as $p) {
                $no++;
                $row = array();

                $row[] = "<input type='checkbox'  class='deleteRow' value='".$row['category_id']."'  /> #".$no ;
                $row[] = $p->category_id;

                // DISPLAY TABLE HIERARCHY ----
                $y=($p->is_parent !=0)?$parenrarray[$p->is_parent]:$p->category_name;
                $x=$p->is_child !=0 ?$parenrarray[$p->is_child]:($p->is_parent !=0 ?$p->category_name:'0');
                $z=$p->is_child !=0 ?$p->category_name:'0';

                $row[] = "<a href='".base_url()."admin/category/level1/$p->category_name'>".$y.'</a>';
                $row[] = "<a href='".base_url()."admin/category/level1/$p->category_name'>".$x.'</a>';
                $row[] = "<a href='".base_url()."admin/category/level1/$p->category_name'>".$z.'</a>';

                //  DATA ACTIONS-------
                $row[] = '<a class="btn btn-xs btn-primary" href="javascript:void()" title="Edit" onclick="edit_person('."'".$p->category_id."'".')">
                      <i class="glyphicon glyphicon-pencil"></i> Edit</a>
                      <a class="btn btn-xs btn-danger" href="javascript:void()" title="Hapus" onclick="delete_person('."'".$p->category_id."'".')">
                      <i class="glyphicon glyphicon-trash"></i> Delete</a>';
                $data[] = $row;   

            }

            $output = array(
                            "draw" => $_POST['draw'],
                            "recordsTotal" => $this->categories->count_all(),
                            "recordsFiltered" => $this->categories->count_filtered(),
                            "data" => $data,
                    );
            //output to json format
            echo json_encode($output);
        }

答案 2 :(得分:0)

多级别类别功能应该遵循递归概念。以下是codeigniter

的示例

模型:

['000', '0000', '000000']

通过调用此(控制器)

public function get_categories(){

    $this->db->select('*');
    $this->db->from('categories');
    $this->db->where('parent_id', 0);

    $parent = $this->db->get();

    $categories = $parent->result();
    $i=0;
    foreach($categories as $p_cat){

        $categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
        $i++;
    }
    return $categories;
}

public function sub_categories($id){

    $this->db->select('*');
    $this->db->from('categories');
    $this->db->where('parent_id', $id);

    $child = $this->db->get();
    $categories = $child->result();
    $i=0;
    foreach($categories as $p_cat){

        $categories[$i]->sub = $this->sub_categories($p_cat->cat_id);
        $i++;
    }
    return $categories;       
}

这是一个官方的codeigniter论坛帖子 https://forum.codeigniter.com/thread-69149.html