2014-04-04 36 views
0

我將$數據傳遞到每個頁面上的一個視圖,並有一個自動加載到位。的觀點之前,我有以下幾點:codeigniter頭文件模板加載模型函數

$data['category'] = $this->productModel->listCategories(); 

我試圖找到一種方法,我不必有這樣的每個頁面視圖控制器上。我試過將代碼放在header.php模板中,但是我得到未定義的變量類別。

裏面的頭部模板,我有:

<?php foreach ($category as $cat) : ?> 
      <li><a href="/products/<?php echo url_title($cat->ProductCategory, '-', TRUE); ?>"><?php echo $cat->ProductCategory; ?></a></li> 
     <?php endforeach; ?> 

完全難住了!

我需要它在每個頁面上運行,因爲它在標題中。

回答

1

把這一行放在控制器的構造函數中,並在任何視圖中引用它在同一控制器中的函數加載。

$ query_result = $ this-> productModel-> listCategories();

將查詢結果分配給全局變量,並在您工作的控制器的任何視圖中使用$ this-> load-> vars($ query_result)。