简体   繁体   English

如何从数据库表创建列表视图?

[英]How to create a list view from a database table?

I'm querying a category table id, parent_id, name to generate list view of all the categories and sub-categories.我正在查询类别表id, parent_id, name以生成所有类别和子类别的列表视图。 What I have so far is:到目前为止,我所拥有的是:

  • HP生命值
    • Laptops笔记本电脑
    • PC个人电脑
  • Sports运动的
    • Sunglasses太阳镜
  • Food食物
  • House房子

The function that I use to generate the output is:我用来生成 output 的 function 是:

public function get_category($parent_id = NULL)
{ 
    $query = $this->db->get_where('category', array('parent_id' => $parent_id))->result();
    $result = '';

    foreach ($query as $row)
    {
        $i = 0;

        if ($i == 0) 
        {
            $result .= '<ul>';  
        }

        $result .= '<li><a href="'.base_url().'category/edit/'.$row->id.'">' . $row->name;
        $result .= $this->get_category($row->id);
        $result .= '</li>';

        $i++;

        if ($i > 0) 
        {
            $result .= '</ul>';
        }
    }

    return $result;
}

This is what I want to achieve:这就是我想要实现的目标:

HP
HP > Laptops
HP > PC
Sports
Sports > Sunglasses
Food
House

Try following尝试关注

public function get_category($parent_id = NULL,$level, $prev = "")
{ 
    $query = $this->db->get_where('category', array('parent_id' => $parent_id))->result();
    $result = '';

    foreach ($query as $row)
    {
       $temp = $prev;
       if($level == 0){
        $temp .= '<a href="'.base_url().'category/edit/'.$row->id.'">' . $row->name.'</a>';
       }else{
        $temp .= ' &gt; <a href="'.base_url().'category/edit/'.$row->id.'">' . $row->name.'</a>';
       }
        $result .= $temp;
        $result .= "<br />";
        $result .= $this->get_category($row->id, $level + 1, $temp);

    }

    return $result;
}

First run of this function should be这个 function 的第一次运行应该是

get_category(NULL, 0, "");

Here这里

  • NULL is parent id (you can use a loop to dynamicaly pass id) NULL是父 id(您可以使用循环动态传递 id)
  • level should be 0 level应为0
  • prev should be empty prev一个应该是空的

I've created a demo according to your needs and it worked for me.我根据您的需要创建了一个演示,它对我有用。 I'm sharing the code below, I've done explaining in the comments wherever necessary.我正在分享下面的代码,我已经在评论中进行了必要的解释。 See if it helps you.看看对你有没有帮助。

Controller Controller

public function get_category($parent_id = NULL){ 

    // get the {category}, {sub_category} from the table, if {sub_category} stored in different table use {JOIN}
    $data['category'] = $this->db->get_where('category', array('parent_id' => $parent_id))->result();  

    $this->load->view('your-view', $data); 
}

View看法

<?php 

if(!empty($category)){ // check if {$category} has value; if not, do nothing

    $catArr = array();  // initialize array to store category id

?>

<ul>  <!-- Start ul -->

<?php 

    foreach($category as $cat){ //loop through all the {category} array

        // check if the {cat_id} already exists in array; if not, do so where {cat_id} is category id(change your value here)
        if( ! in_array( $cat->cat_id, $catArr ) ){ 

            $catArr[] = $cat->event_id;

        ?>  

        <li><?php echo $cat->cat_name; ?></li>  <!-- show {cat_name} in list -- your category name here -->

        <?php

        }

        ?>

    <!-- Show {cat_name} and {subcat_name} both -- (your values here). Give links or whatever here(your styling)-->
    <li><?php echo $cat->cat_name; ?> &gt; <?php echo $cat->subcat_name; ?></li>

    <?php

    } 

    ?>

</ul>

<?php 

} 

?>

OUTPUT OUTPUT

 Honda Honda > Car Honda > Bike Philips Philips > Electricals Philips > Electronics

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM