子类别的名称在列表中未显示正确

时间:2015-09-22 02:42:07

标签: php codeigniter

我正在为我的项目创建一个类别列表。

当我在我的管理员上查看我的列表时,由于某种原因,子类别将父类别显示为子类别?父类别在图像中右侧有雪佛龙。

问题如何确保在父类别下面显示正确的子类别。

enter image description here

  

每个子类别在我的数据库表类别

中都有一个parent_id

类别表

enter image description here

类别说明表

enter image description here

控制器

<?php

class Category extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin/catalog/model_category');
    }

    public function add() {
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $this->model_category->add();
            redirect('admin/catalog/category');
        }

        $this->get_form();
    }

    public function edit() {
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            $this->model_category->edit();
            redirect('admin/catalog/category');
        }

        $this->get_form();
    }

    public function index() {
        $this->get_list();
    }

    public function get_list() {
        $this->document->setTitle('Categories');

        $data['heading_title'] = 'Categories';

        $data['categories'] = array();

        $results = $this->model_category->getCategories();

        foreach($results as $result){

            $sub_results = $this->model_category->getSubcategories($result['category_id']);

            foreach ($sub_results as $sub_result) {
                $data['subcategories'][] = array(
                    'category_id' => $result['category_id'],
                    'name' => $result['name']
                );
            }

            $data['categories'][] = array(
                'category_id' => $result['category_id'],
                'name' => $result['name']
            );
        }

        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $this->load->view('template/catalog/category_list_view', $data);
    }
}

模型

<?php

class Model_category extends CI_Model {

    public function get_category() {
        $this->db->select('*');
        $this->db->from($this->db->dbprefix . 'category');
        $this->db->where('category_id', $this->uri->segment(5));
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return FALSE;
        }
    }

    public function getCategories() {
        $this->db->select('*');
        $this->db->from($this->db->dbprefix . 'category_description cd', 'LEFT');
        $this->db->join($this->db->dbprefix . 'category c', 'c.category_id = cd.category_id', 'LEFT');
        $this->db->where('c.parent_id', '0');
        $query = $this->db->get();
        return $query->result_array();
    }

    function getSubcategories($parent_id) {
        $this->db->select('*');
        $this->db->from($this->db->dbprefix . 'category_description');
        $this->db->where('category_id', $parent_id);
        $query = $this->db->get();
        return $query->result_array();
    } 
}

查看

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h4>Categories</h4>
<div class="list-group categories">
<?php foreach($categories as $category) { ?>

<div class="list-group-item"><?php echo $category['name']; ?><span class="glyphicon glyphicon-chevron-right"></span></div>

<?php foreach($subcategories as $subcategory) { ?>
<div class="list-group-item">
<?php echo $subcategory['name']; ?>
</div>
<?php }?>

<?php }?>
</div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

感谢大家的建议。

我已经找到了创造我所追求的最佳方式。

  

这是输出放置视图图像

enter image description here

首先,我创建了一个表,而不是拥有两个表,以便于它。

然后在我的模型上我将我的功能改为

function get_categories(){
    return $this->db->get_where('category', array('parent_id' => '0'))->result_array();
}

function get_sub_categories($category_id){
    return $this->db->get_where('category', array('parent_id' => $category_id))->result_array();
}

然后在控制器上

<?php

class Category extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin/catalog/model_category');
    }


    public function index() {
        $this->get_list();
    }

    public function get_list() {
        $this->document->setTitle('Categories');

        $data['heading_title'] = 'Categories';

        $data['categories'] = array();

        $categories = $this->model_category->get_categories();

        foreach ($categories as $category) {

            $sub_category_data = array();

            $sub_categories = $this->model_category->get_sub_categories($category['category_id']);

            foreach ($sub_categories as $sub_category) {
                $sub_category_data[] = array(
                    'category_id' => $sub_category['category_id'],
                    'name' => $category['name'] . '&nbsp;&nbsp;&gt;&nbsp;&nbsp;' . $sub_category['name'],
                    'edit' => site_url('admin/catalog/category/edit/' . $sub_category['category_id'])
                );
            }

            $data['categories'][] = array(
                'category_id' => $category['category_id'],
                'name' => $category['name'],
                'sub_categories' => $sub_category_data,
                'edit' => site_url('admin/catalog/category/edit/' . $category['category_id'])
            );
        }

        $data['header'] = Modules::run('admin/common/header/index');
        $data['footer'] = Modules::run('admin/common/footer/index');

        $this->load->view('template/catalog/category_list_view', $data);
    }
}

然后在视图

<?php echo $header;?>
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="page-header">
<h1><?php echo $heading_title;?></h1>
</div>
</div>
</div>

<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="panel panel-default">

<div class="panel-heading">
<div class="clearfix">
<div class="pull-left">
<h1 class="panel-title"></h1>
</div>
<div class="pull-right">
<a href="<?php echo base_url('admin/catalog/category/add');?>" role="button" class="btn btn-success">Add New Category</a>
</div>
</div>
</div>

<div class="panel-body">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td>Category Name</td>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
<?php if ($categories) {?>
<?php foreach($categories as $category) : ?>
<tr class="category">
<td><?php echo $category['name']; ?></td>
<td class="text-right"><a href="<?php echo $category['edit'];?>" role="button" class="btn btn-primary">Edit</a></td>
</tr>
<?php foreach($category['sub_categories'] as $subcategory) : ?>
<tr>
<td><?php echo $subcategory['name']; ?></td>
<td class="text-right"><a href="<?php echo $subcategory['edit'];?>" role="button" class="btn btn-primary">Edit</a></td>
</tr>
<?php endforeach;?>
<?php endforeach;?>
<?php }?>
</tbody>
</table>
</div>

<div class="panel-footer">
</div>

</div>
</div>
</div>

</div>
<?php echo $footer;?>