Codeigniter框架中的子类别子类别

时间:2017-06-08 22:15:31

标签: php codeigniter

我想在主要类别中显示子类别。我的表格看起来像this。但是当我print_r($ cat_id)时,它只返回该类别的最后一个id。我怎样才能解决这个问题?这是我的控制器:

$data['categories'] = $this->courses_model->get_all_categories_for_courses();

            foreach($data['categories'] as $ct){

                        $cat_id = $ct['id'];        
                }



            $data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_id);

            $this->load->view('templates/header');
            $this->load->view('courses/courses-grid-fullwidth', $data);
            $this->load->view('templates/footer');
        }

这是我的模特:

public function get_all_categories_for_courses() {
        $query = $this->db->get_where('categories', array('parent_id' => NULL));
        return $query->result_array();
    }

    function get_all_subcategories_for_courses($cat_id) {
        $query = $this->db->get_where('categories', array('parent_id' => $cat_id));
        return $query->result_array();
    } 

这是我的观点页面:

<?php foreach($categories as $cat): ?>
          <div class="col-md-3 col-sm-6 incat">

             <h4>
               <a href="#" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
               <?= $cat['name_rus']; ?></a>               
             </h4>

            <ul>
              <?php foreach($subcategories as $subcat): ?>
              <li><a href="#"><?= $subcat['name_rus']; ?></a></li>
              <?php endforeach; ?>
            </ul>
          </div>
        <?php endforeach; ?>

提前致谢!

2 个答案:

答案 0 :(得分:1)

正如Ukasyah在对您的问题的评论中所说,每次循环浏览类别时,您都会覆盖$cat_id值。

您需要做的是获取一个填充了ID的数组,并使用$this->db->where_in()匹配数组而不是单个值。

您的控制器需要看起来更像这样:

$data['categories'] = $this->courses_model->get_all_categories_for_courses();

$cat_ids = [];
foreach($data['categories'] as $ct){

    $cat_ids[] = $ct['id'];
}

$data['subcategories'] = $this->courses_model->get_all_subcategories_for_courses($cat_ids);

$this->load->view('templates/header');
$this->load->view('courses/courses-grid-fullwidth', $data);
$this->load->view('templates/footer');

虽然您需要将模型功能更改为:

function get_all_subcategories_for_courses($cat_ids) {
    $query = $this->db->where_in('parent_id', $cat_ids)->get('categories');
    return $query->result_array();
}

祝你好运。

答案 1 :(得分:1)

或者你可以这样做

<?php foreach($categories as $cat): ?>
          <div class="col-md-3 col-sm-6 incat">

             <h4>
               <a href="<?= site_url('/courses/category/'.$cat['category_slug']); ?>" class="catname"><i class="fa <?= $cat['icon']; ?>" aria-hidden="true"></i>
               <?= $cat['name_rus']; ?></a>               
             </h4>

            <ul>
            <?php 
            $query = $this->db->get_where('categories', array('parent_id' => $cat['id']));
            ?>

              <?php foreach($query->result() as $subcat): ?>
              <li><a href="<?= site_url('/courses/category/'.$subcat->category_slug); ?>"><?= $subcat->name_rus; ?></a></li>
              <?php endforeach; ?>
            </ul>
          </div>
        <?php endforeach; ?>