2010-01-02 56 views
0

我正在構建一個系統,用戶可以從一系列catergories中構建自己的導航菜單,它的工作方式是,他們點擊'Blog',他們可以得到一部名爲手風琴的標題'博客',並擴大這一點,他們看到所有的博客標題,但是現在,如果有多個博客條目,我的應用程序會創建多個手風琴,所以目前我有兩個博客條目,當用戶發佈博客時,以手風琴,在那裏,他們應該在填充手風琴與數據庫結果幫助

控制器得到一個與所有的標題:

public function category($content_id) { 
    //$this->output->enable_profiler(TRUE); 
    if (intval($this->uri->segments[4])){ 
     $content_id = $this->uri->segments[4]; 
    } else { 
     $content_id = $this->uri->segments[7]; 
    } 
    $data['content'] = $this->site_model->get_content($content_id); 
    $this->load->view("call", $data); 
} 

型號:

public function get_content($content_id) { 
    $this->db->select('*'); 
    $this->db->from ('content'); 
    $this->db->join('categories', 'categories.category_id = content.categories_category_id', 'left'); 
    $this->db->where ('categories_category_id', $content_id); 

    $query = $this->db->get(); 
    return $query->result_array(); 

} 

查看:

<?php 
if(isset($content)) { 
// var_dump($content); 
    foreach($content as $row) { 
     echo "<h2 class='$row[category_name]'><a href='#'>$row[category_name]</a></h2>"; 
     echo "<div class='$row[category_name]'><a href='index.php/home/get_content/$row[content_id]' class='contentlink'>$row[content_title]</a></div>"; 
    } 
} 
?> 

我怎樣才能改變我的代碼,以便爲類用戶僅選擇一個手風琴被內置放realted內容被放置在手風琴的所有類別?

希望這是有道理的。

回答

0

如果我理解正確,你應該移到頭的創建出你的循環:

<?php 
if(isset($content)) { 
    echo "<h2 class='$category_name'><a href='#'>$category_name</a></h2>"; 
    foreach($content as $row) { 
     echo "<div class='$row[category_name]'><a href='index.php/home/get_content/$row[content_id]' class='contentlink'>$row[content_title]</a></div>"; 
    } 
} 
?> 

如果你不知道的類別名稱,你可以嘗試這樣的事:

<?php 
$first = true; 
if(isset($content)) { 
    echo "<h2 class='$category_name'><a href='#'>$category_name</a></h2>"; 
    foreach($content as $row) { 
     if ($first) { 
      echo "<h2 class='$row[category_name]'><a href='#'>$row[category_name]</a></h2>"; 
      $first = false; 
     } 
     echo "<div class='$row[category_name]'><a href='index.php/home/get_content/$row[content_id]' class='contentlink'>$row[content_title]</a></div>"; 
    } 
} 
?>