* foreach inside foreach codeigniter 2? [英] * foreach inside foreach codeigniter 2?

查看:151
本文介绍了* foreach inside foreach codeigniter 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在codeigniter 2.1中,我试图按类别显示频道。所以如果我有一个类别叫Film,我应该看到电影中的频道列表。我尝试了一个嵌套的foreach循环来完成这个工作,但似乎不能让它工作。

In codeigniter 2.1 I'm trying to display channels by category. So if i have a category called Film, i should see a list of Channels within Film. I tried a nested foreach loop to accomplish this but can't seem to get it to work.

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

My tables structure is something like this but more complicated:


  • 我的模型:

  • My model:

<?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();
}

}


  • in the 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(Pages):

  • controller (Pages):

    public function index()
    {
    
    $data['category_chaneels'] = $this->pages_model->get_channels_by_categ_tv();
    
    $this->template->page_view($data);
    }
    


  • 和图像2,我需要结果像图像2不是1。

    I atached image 1 and image 2, i need result like image 2 not 1.

    PS。一个频道可以有多个类别。

    PS. One channel can have many categories.


    你能帮助我吗? THX

    Can you help me ? THX

    推荐答案

    我的最终代码是这样的。 Maby sombody需要这个。

    My final code is this. Maby sombody need this.

        <div id="programe-tv-slide" class="flexslider">
            <strong>Programe TV</strong>
            <div class="redLine"></div>
    
            <?php $cat_cnl = array(); 
                  $list = array(); 
                  $i=1;
                     foreach ($category_chaneels as $option) { 
                        $catname = $option['category_name'];
                        $chlname = $option['channel_name'];
    
                        $cat_cnl[$catname][$i] = $chlname;
                        $list[$i] = $catname;
                  $i++;
                     }; 
            ?>
            <?php 
                $rows = array_chunk($cat_cnl, 4, TRUE);
                foreach ($rows as $row) {  //var_dump($rows);
            ?>
    
              <ul class="slides">    
                <?php
                    echo ('<li>');
                    foreach ($row as $category => $channels) {
                        echo '<div class="programe-tv_link">'; 
                        echo '<p>' . $category . '</p>'; 
                            foreach ($channels as $channel) {
                                    echo '<dd><a href="">' . $channel . '</a></dd> '; 
                            };
                        echo '</div>'; 
                        };      
                    echo ('</li>'); 
                ?>
              </ul>
              <?php }; ?>
        </div>
    

    这篇关于* foreach inside foreach codeigniter 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆