2011-08-21 121 views
0

我試圖拿我的結果集並將其發送到視圖,以便它可以相應地顯示,我有問題了解在這裏找到的文檔我有一個模板視圖我希望它以某種方式顯示如下:這只是給你一個想法,在模板下面或者是什麼不是我的控制器的代碼和從數據庫獲取類別名稱的模型,還包括我目前的我的看法(PHP語法錯誤)通過數據庫和配置文件創建動態鏈接

http://codeigniter.com/user_guide/database/results.html

<div class="menu"> 
     <ul class="clear"> 
      <li class="active"><a href="/">Dashboard</a></li> 
      <li><?php echo anchor('cpanel/modules/articles', 'Articles'); ?></li> 
      <li><a href="styles.html">Styles</a></li> 
      <li><a href="tables.html">Tables</a></li> 
      <li><a href="charts.html">Charts</a></li> 
      <li><a href="gallery.html">Image Gallery</a></li> 
      <li><a href="settings.html">Settings</a></li> 
     </ul> 
    </div> 

控制器:

function index() 
{ 
    $id = $this->tank_auth->get_user_id(); 
    $data = $this->Dashboard->get_user_info($id); 
    $rows = $this->Dashboard->get_menu_categories(); 
    $this->template->set_layout('cpanel')->enable_parser(false); 
    $this->template->set('data', $data); 
    $this->template->set('menu_categories', $rows); 
    $this->template->set_partial('header', 'partials/header'); 
    $this->template->set_partial('sidebar', 'partials/sidebar'); 
    $this->template->set_partial('content', 'partials/content');  
    $this->template->set_partial('footer', 'partials/footer'); 
    $this->template->build('/cpanel/index'); 
} 

型號:

/** 
* Get menu categories 
* 
* @param none 
* @param none 
* @return object 
*/ 
function get_menu_categories() 
{ 
    $this->db->select('*'); 
    $this->db->from('menu_categories'); 
    $this->db->where('status_id', '1'); 
    $this->db->order_by('sort_order', 'desc'); 

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

    return $row = $query->result_array(); 
} 

查看:

<div class="menu"> 
     <ul class="clear"> 
      <?php 
      foreach ($row as $row) 
      { 
       echo "<li>" anchor('".$row['linkURL']."', '$row['category_name']')"</li>"; 
      } 
      ?>     
     </ul> 
    </div> 

編輯:這是我的新代碼。我的「M得到以下錯誤消息:嚴重性:注意

消息:未定義變量:行

文件名:泛音/ header.php文件

行號:遇到34 甲PHP錯誤

嚴重性:警告

消息:提供的foreach無效的參數()

費爾爲ename:諧音/ header.php文件

行號:34

控制器:

function index() 
{ 
    $id = $this->tank_auth->get_user_id(); 
    $data = $this->Dashboard->get_user_info($id); 
    $menu_data['rows'] = $this->Dashboard->get_menu_categories(); 
    $this->template->set_layout('cpanel')->enable_parser(false); 
    $this->template->set('data', $data); 
    $this->template->set('menu_categories', $menu_data); 

    $this->template->set_partial('header', 'partials/header'); 
    $this->template->set_partial('sidebar', 'partials/sidebar'); 
    $this->template->set_partial('content', 'partials/content');  
    $this->template->set_partial('footer', 'partials/footer'); 
    $this->template->build('/cpanel/index'); 
} 

型號:

/** 
* Get menu categories 
* 
* @param none 
* @param none 
* @return object 
*/ 
function get_menu_categories() 
{ 
    $this->db->select('*'); 
    $this->db->from('menu_categories'); 
    $this->db->where('status_id', '1'); 
    $this->db->order_by('sort_order', 'desc'); 

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

    return $query->result_array(); 
} 

查看

<div class="menu"> 
     <ul class="clear"> 
      <?php 
      foreach ($rows as $row) 
      { 
       echo '<li>'.anchor($row['category_name'], $row['category_name']).'</li>'; 
      } 
      ?>     
     </ul> 
    </div> 
+1

在視圖中:foreach(** $ rows ** as $ row) –

+1

當您使用第三方庫(如您似乎有的模板庫)時,您應該指定並鏈接到文檔,這需要大量的猜測工作的回答。 –

+0

我會下次。 –

回答

2

更改的最後一行get_menu_categories()return $query->result_array();,這裏的$row變量不會影響返回值,也不會執行函數範圍之外的任何操作。

假設$this->template->set()需要關聯數組作爲第二個參數,這是有可能的情況下,更改爲:

$menu_data['rows'] = $this->Dashboard->get_menu_categories(); 
//   ^^^^ This is your variable name to be used in the view 
$this->template->set('menu_categories', $menu_data); 

然後,你應該能夠從menu_categories視圖訪問$rows

正如在評論中指出,使用$rows小號

foreach ($rows as $row) 
{ 
    echo '<li>'.anchor($row['linkURL'], $row['category_name']).'</li>'; 
} 
+0

我收到一條錯誤消息,說試圖在部分視圖上的foreach循環中獲取非對象的屬性。 –

+1

有了這個確切的代碼?沒有意義,它不是試圖訪問一個對象屬性,而是一個數組值。 –

+0

是的,這是真的。 –