2011-10-14 53 views
0

由於某種原因,我在視圖中獲取了一個未定義的變量,並不確定原因。當我做一個footer_links變量的print_r時,它顯示正常:未定義變量footer_links

Array([0] => stdClass Object([link_name] => Home [short_name] => index)1 => stdClass Object [link_name] =>關於我們[short_name] => about-us)[2] => stdClass Object([link_name] => Site Map [short_name] => site-map)[3] => stdClass Object([link_name ] =>聯繫我們[=]聯繫我們)[4] => stdClass對象([link_name] =>新聞饋送[短名] =>新聞饋送))

我知道我不是傳遞變量,但當我嘗試與構建它沒有工作。不知道爲什麼我應該把它傳入。我正在使用Phil Sturgeon的模板庫。

A PHP Error was encountered 

Severity: Notice 

Message: Undefined variable: footer_links 

Filename: v1/footer.php 

Line Number: 4 
A PHP Error was encountered 

Severity: Warning 

Message: Invalid argument supplied for foreach() 

Filename: v1/footer.php 

Line Number: 4 

控制器:

$activeTemplate = $this->kow->getTemplate(); 
$siteInfo = $this->kow->getSiteTitleAndSlogan(); 
$footer_links = $this->kow->getFooterNav(); 
$this->template 
    ->title($siteInfo[0]->site_name,$siteInfo[0]->site_slogan) 
    ->prepend_metadata('<link rel="stylesheet" type="text/css" href="http://www.kansasoutlawwrestling.com/assets/css/'.$activeTemplate[0]->short_name.'.css" />') 
    ->set_partial('header', $activeTemplate[0]->short_name.'/header') 
    ->set_partial('footer', $activeTemplate[0]->short_name.'/footer') 
    ->build('kow'); 

查看:

<div id="footer"> 

    <ul> 
     <?php foreach($footer_links as $row) 
     { 
     ?> 
     <li><a><?php echo $row->link_name; ?></a></li> 
     <?php 
     } 
     ?> 
    </ul> 

    <p>&copy;COPYRIGHT 2010 ALL RIGHTS RESERVED</p> 

</div> 

編輯:

我發現這個計算器的問題是同一類交易我與...合作。 Creating Dynamic Links Through DB and CI

控制器:

$activeTemplate = $this->kow->getTemplate(); 
$siteInfo = $this->kow->getSiteTitleAndSlogan(); 
$footer_links['rows'] = $this->kow->getFooterNav(); 
$this->template 
->title($siteInfo[0]->site_name,$siteInfo[0]->site_slogan) 
->prepend_metadata('<link rel="stylesheet" type="text/css" href="http://www.kansasoutlawwrestling.com/assets/css/'.$activeTemplate[0]->short_name.'.css" />') 
->set('footer', $footer_links)    
->set_partial('header', $activeTemplate[0]->short_name.'/header') 
->set_partial('footer', $activeTemplate[0]->short_name.'/footer') 
->build('kow'); 

型號:

function getFooterNav() 
{ 
    $this->db->select('site_menu_structures_links.link_name,site_menu_structures_links.short_name'); 
    $this->db->from('site_menu_structures_links'); 
    $this->db->join('site_menu_structures', 'site_menu_structures.id = site_menu_structures_links.menu_structure_id'); 
    $this->db->where('site_menu_structures.short_name', 'footernav'); 
    $query = $this->db->get(); 
    return $query->result_array(); 
} 

查看:

<div id="footer"> 

    <ul> 
     <?php foreach($rows as $row) 
     { 
     ?> 
     <li><a><?php echo $row->link_name; ?></a></li> 
     <?php 
     } 
     ?> 
    </ul> 

    <p>&copy;COPYRIGHT 2010 ALL RIGHTS RESERVED</p> 

</div> 

回答

1

我不熟悉的你正在使用的模板引擎,但你如何將你的變量傳遞給你的視圖?你在控制器內部做print_r(當然這裏會顯示)還是視圖?嘗試將var_dump($footer_links); exit;作爲您的footer.php文件中的第一行。

+0

我更新了我的帖子。 –

+0

仔細看看回答該問題的人的第一個代碼塊。我相信這就是你做錯了什麼。它現在工作嗎?請現在發佈錯誤(如果有的話),現在你已經改變了它。 – Spyros