2013-01-11 84 views
3

在codeigniter 2.1中,我試圖按類別顯示頻道。所以如果我有一個名爲Film的類別,我應該看到電影中的頻道列表。我嘗試了一個嵌套的foreach循環來實現這一點,但似乎無法讓它工作。* foreach內部foreach codeigniter 2?

我的表結構是這樣的,但更復雜:

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(); 
    } 
    
    } 
    
  • 視圖

      <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> 
    
  • 控制器(頁數):

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

我atached圖像1和圖像2,我需要導致像圖像2而不是1

PS。一個頻道可以有很多類別。

enter image description here 你能幫我嗎? THX

回答

1

我最後的代碼是這樣的。 Maby sombody需要這個。

<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> 
2

在您看來,嘗試

<?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> 
+0

嗨stealthyninja,我該如何查看diferents DIV和限制1格 - > 3裏, –