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

我正在查询类别表id, parent_id, name以生成所有类别和子类别的列表视图。到目前为止我所拥有的是:

  • 生命值

    • 笔记本电脑

    • 个人电脑

  • 运动的

    • 太阳镜

  • 食物

  • 房子

我用来生成输出的函数是:

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;

}

这就是我想要实现的目标:


HP

HP > Laptops

HP > PC

Sports

Sports > Sunglasses

Food

House


紫衣仙女
浏览 109回答 2
2回答

婷婷同学_

尝试以下public function get_category($parent_id = NULL,$level, $prev = ""){&nbsp;&nbsp; &nbsp; $query = $this->db->get_where('category', array('parent_id' => $parent_id))->result();&nbsp; &nbsp; $result = '';&nbsp; &nbsp; foreach ($query as $row)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;$temp = $prev;&nbsp; &nbsp; &nbsp; &nbsp;if($level == 0){&nbsp; &nbsp; &nbsp; &nbsp; $temp .= '<a href="'.base_url().'category/edit/'.$row->id.'">' . $row->name.'</a>';&nbsp; &nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; $temp .= ' &gt; <a href="'.base_url().'category/edit/'.$row->id.'">' . $row->name.'</a>';&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; $result .= $temp;&nbsp; &nbsp; &nbsp; &nbsp; $result .= "<br />";&nbsp; &nbsp; &nbsp; &nbsp; $result .= $this->get_category($row->id, $level + 1, $temp);&nbsp; &nbsp; }&nbsp; &nbsp; return $result;}这个函数的第一次运行应该是get_category(NULL, 0, "");这里NULL是父 ID(您可以使用循环动态传递 ID)level应该0prev应该是空的

达令说

我根据您的需要创建了一个演示,它对我有用。我正在分享下面的代码,我已经在必要时在评论中进行了解释。看看它是否对你有帮助。控制器public function get_category($parent_id = NULL){&nbsp;&nbsp; &nbsp; // get the {category}, {sub_category} from the table, if {sub_category} stored in different table use {JOIN}&nbsp; &nbsp; $data['category'] = $this->db->get_where('category', array('parent_id' => $parent_id))->result();&nbsp;&nbsp;&nbsp; &nbsp; $this->load->view('your-view', $data);&nbsp;}看法<?php&nbsp;if(!empty($category)){ // check if {$category} has value; if not, do nothing&nbsp; &nbsp; $catArr = array();&nbsp; // initialize array to store category id?><ul>&nbsp; <!-- Start ul --><?php&nbsp;&nbsp; &nbsp; foreach($category as $cat){ //loop through all the {category} array&nbsp; &nbsp; &nbsp; &nbsp; // check if the {cat_id} already exists in array; if not, do so where {cat_id} is category id(change your value here)&nbsp; &nbsp; &nbsp; &nbsp; if( ! in_array( $cat->cat_id, $catArr ) ){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $catArr[] = $cat->event_id;&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <li><?php echo $cat->cat_name; ?></li>&nbsp; <!-- show {cat_name} in list -- your category name here -->&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; <!-- Show {cat_name} and {subcat_name} both -- (your values here). Give links or whatever here(your styling)-->&nbsp; &nbsp; <li><?php echo $cat->cat_name; ?> &gt; <?php echo $cat->subcat_name; ?></li>&nbsp; &nbsp; <?php&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; ?></ul><?php&nbsp;}&nbsp;?>输出HondaHonda > CarHonda > BikePhilips&nbsp;Philips > ElectricalsPhilips > Electronics
打开App,查看更多内容
随时随地看视频慕课网APP