显示数据库php的嵌套列表

时间:2016-04-21 02:29:57

标签: php codeigniter

很抱歉再次提出同样的问题。我发现一些与我的问题相关但没有解决我的问题。我知道如何做到这一点的逻辑,但问题是我无法在我的代码中得到它。

我想显示Items表中的所有项目。具有type = section的项目将是所有子项目的父项。现在,如何将指定父列表中的所有项目分组并将其显示为父项下面的子列表?

控制器:

public function show_item () { 
  $data = array(
   'items' => $this->checklist_item_model->get_specific_item($id),
   'checklist_info' => $this->checklist_item_model->get_checklist_info($id)
   );
}

型号:

 public function get_specific_item ($id) {
  $output = $this->db->get_where('checklist_items', array('checklist_id' => $id));
  return $output->result_array();
 }

 public function get_checklist_info($id) {
  $query = $this->db->get_where('checklists', array('id' => $id));
  $result = $query->result_array ();
  return $result;
 }

查看:

<ul class="list-group list-group-sortable list-group-root">
<?php foreach ($items as $key => $value):?>
  <li class="list-group-item" draggable="true">
  <?php echo $value['type'] == 'section' ? $value['title'] : '' //display the parent items where type is equal to section?> 
  </li>
   <ul class="list-group list-group-sortable">
   <li class="list-group-item list-group-sortable"><i class="fa fa-ellipsis-v"></i>
    <span class="badge <?php echo 'badge'.'-'.$value['status']; ?>">
    <?php  echo ($value['status'] == 'pending' ? 'Pending' : ($value['status'] == 'complete' ? 'Complete' : 'Incomplete')) ?>
    </span>
    <a href="#" onclick="manage_item('<?php echo count($items); ?>, <?php echo $value['title'] ?>')">
    <?php if($value['type'] !== 'section') {
     echo $value['title']; //display all items as a sublist belong to a parent list
     } ?>
     </a>
    </li> 
   </ul>                       
 <?php endforeach; ?>

Cheklist_item表: Networking Device Classes

当项目等于0时,是否可以将parent_id始终等于0? 请注意,order_no将是其父级列表的顺序。 非常感谢任何帮助。

0 个答案:

没有答案