gpt4 book ai didi

codeigniter - * foreach 里面的 foreach codeigniter 2?

转载 作者:行者123 更新时间:2023-12-02 00:13:37 25 4
gpt4 key购买 nike

在 codeigniter 2.1 中,我试图按类别显示 channel 。因此,如果我有一个名为 Film 的类别,我应该会在 Film 中看到一个 channel 列表。我尝试了嵌套的 foreach 循环来完成此操作,但似乎无法正常工作。

我的表结构是这样的,但更复杂:

Database structure

  • 我的模型:

    <?php

    class Pages_model extends CI_Model {


    function get_channels_by_categ_tv()
    {

    $this->db->select('categories.category_name, channels.channel_name');
    $this->db->from('type_categ');
    $this->db->join('categories', 'categories.category_id = type_categ.category_id');
    $this->db->join('channels', 'channels.channel_id = type_categ.channel_id');
    $this->db->order_by('categories.category_id');
    //$this->db->group_by(array('categories.category_id'));
    $query = $this->db->get();

    if($query->num_rows() == 0)
    {
    #no channels
    return false;
    }

    return $query->result_array();
    }

    }
  • 在 View 中:

              <ul class="slides">
    <li>
    <?php foreach ($category_chaneels as $category): ?>
    <div class="programe-tv_link">
    <p><?=$category['category_name'];?></p>
    <dd> <a href=""> >> <?=$category['channel_name'];?></a></dd>
    </div>
    <?php endforeach; ?>
    </li>
    </ul>
  • Controller (页面):

    public function index()
    {

    $data['category_chaneels'] = $this->pages_model->get_channels_by_categ_tv();

    $this->template->page_view($data);
    }

我附上了图片 1 和图片 2,我需要的是图片 2 而不是图片 1 的结果。

附言。一个 channel 可以有多个类别。

enter image description here你能帮助我吗 ?谢谢

最佳答案

在你看来,试试

<?php $cat_shown = ''; ?>
<div class="programe-tv_link">
<?php foreach ($category_chaneels as $category): ?>
<?php
if ($cat_shown != $category['category_name']) {
echo '<p>' . $category['category_name'] . '</p>';
$cat_shown = $category['category_name'];
}
?>
<dd><a href=""> >> <?=$category['channel_name'];?></a></dd>
<?php endforeach; ?>
</div>

关于codeigniter - * foreach 里面的 foreach codeigniter 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14276883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com