2015-10-19 109 views
0

我一直在爲最後2個小時努力尋找爲什麼我得到這個錯誤,但我仍然卡住!我已經在這裏檢查了答案,但仍然沒有。CodeIgniter PHP未定義變量錯誤

檢查類似的線程後,我似乎無法找到一個解決方案

幫助將不勝感激

錯誤信息:遇到

一個PHP錯誤

嚴重性:注意

消息:未定義的變量:UPGRADES

文件名:觀點/ test.php的

行號:207

型號:

<?php 

if(!defined('BASEPATH')) exit('Hacking Attempt: Get out of the system ..!'); 

class M_live extends CI_Model { 

     function M_live(){ 
      parent::Model(); 

     function getUpgrades(){ 

      $this->db->select("*"); 
      $this->db->from('client_trust_versions') 

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

控制器:

<?php 


if(!defined('BASEPATH')) exit('Hacking Attempt: Get out of the system ..!'); 

class C_Live extends CI_Controller 
{  

function C_Live(){ 
    parent::__construct(); 
    $this->load->model('M_live'); 
} 

public function index(){ 

    $query = $this->M_live->getUpgrades(); 
    $data['UPGRADES'] = null; 
    if($query){ 
     $data['UPGRADES'] = $query; 
    } 

    $this->load->view('Test', $data); 
}  

} 
?> 

查看:

<table> 
    <tr> 
     <th>ID</th> 
     <th>Client Name</th> 
     <th>App Server</th> 
     <th>Instance Name</th> 
     <th>BankStaff Server</th> 
     <th>SQL Server</th> 
     <th>Instance</th> 
     <th>Health Roster Version</th> 
     <th>Employee Online Version</th> 
     <th>Roster Perform</th> 
     <th>BankStaff Version</th> 
     <th>SafeCare Version</th> 
     <th>E-Expenses</th> 
    </tr> 

    <?php foreach((array)$UPGRADES as $upgrades){?> 

     <tr><td><?php=$upgrade->ID;?></td> 
     <td><?php=$upgrade->Client_Name;?></td> 
     <td><?php=$upgrade->App_Server;?></td> 
     <?php }?> 

+0

可能重複[PHP:「注意:未定義的變量」和「注意:未定義的索引」](http://stackoverflow.com/questions/4261133/php-notice-undefined -variable-and-notice-undefined-index) – Qirel

+0

那麼我猜查詢不會返回結果。要測試用$ data ['UPGRADES'] = array();'替換'$ data ['UPGRADES'] = null;'它應該可以工作 – Steve

+0

如果你看一下變量名,你可以使用'foreach'語句中'$ upgrades',但裏面有'$ upgrade'。 – Qirel

回答

0

型號:

<?php 
if(!defined('BASEPATH')) exit('Hacking Attempt: Get out of the system ..!'); 

class M_live extends CI_Model 
{ 
    function M_live() 
    { 
    parent::Model(); 
    } 

    function getUpgrades() 
    { 
    $query = $this->db->get('client_trust_versions'); 
    // Produces: SELECT * FROM client_trust_versions 
    return $query->num_rows() > 0 ? $query->result() : NULL; 
    } 
} 

如果發現行,這將返回的結果作爲對象,並返回NULL,如果沒有行。更改爲返回一個對象而不是數組,因爲這是您在具有$upgrade->key語法的視圖中使用的類型。要使用數組,你會使用$upgrade['key']

在控制器使用這種

public function index(){ 
    $query = $this->M_live->getUpgrades(); 
    if(isset($query)){ 
     $data['upgrades'] = $query; 
    } 
    $this->load->view('Test', $data); 
} 

新視角:

<table> 
<tr> 
    <th>ID</th> 
    <th>Client Name</th> 
    <th>App Server</th> 
    <th>Instance Name</th> 
    <th>BankStaff Server</th> 
    <th>SQL Server</th> 
    <th>Instance</th> 
    <th>Health Roster Version</th> 
    <th>Employee Online Version</th> 
    <th>Roster Perform</th> 
    <th>BankStaff Version</th> 
    <th>SafeCare Version</th> 
    <th>E-Expenses</th> 
</tr> 

<?php 
if(isset($upgrades)){ 
//If no rows were found $upgrades will be NULL and isset() will return false. 
//Therefore, you never try to address an undefined variable. 
    foreach($upgrades as $upgrade){?> 
    <tr><td><?php=$upgrade->ID;?></td> 
    <td><?php=$upgrade->Client_Name;?></td> 
    <td><?php=$upgrade->App_Server;?></td></tr> 
<?php }}?> 
</table> 

通知$ UPGRADES改爲$升級。公約是所有大寫字母名稱都表示一個常數。請注意0​​中複數和單數var名稱的用法,它避免了您可能遇到的名稱衝突。 (並因此全部上限var名稱)

+0

這很好,它的工作,我知道錯誤。只需要獲取屏幕上顯示的數據。再次感謝你的幫助! –

0

修正模型代碼:

<?php 

if(!defined('BASEPATH')) exit('Hacking Attempt: Get out of the system ..!'); 

class M_live extends CI_Model { 

     function M_live(){ 
      parent::Model(); 
     } 

     function getUpgrades(){ 

      $this->db->select("*"); 
      $this->db->from('client_trust_versions') 

      $query = $this->db->get(); 
      return $query->result_array();  
     }  
    } 
?> 
+0

我是否使用我在控制器和視圖中使用的相同代碼?我也在努力讓數據在我看來表現出來。你可以幫忙的事情? –

+0

那麼你可以使用@Dfriend回答.. – Nere