CodeIgniter PHP 未定义变量错误

CodeIgniter PHP Undefined Variable error

在过去的 2 个小时里,我一直在努力找出为什么会出现此错误,但我仍然卡住了!我已经检查了这里的答案,但仍然没有。

在检查了类似的线程后,我似乎找不到解决方案

不胜感激

错误信息:

遇到了一个PHP错误

严重性:通知

消息:未定义的变量:UPGRADES

文件名:views/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 }?>

将您的型号代码修正为:

<?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 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;
  }
}

如果找到行,这将 return 作为对象的结果,如果没有行,则 return NULL。更改为 returning 对象而不是数组,因为这是您在视图中使用 $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 已更改为 $upgrades。约定是所有大写名称表示一个常量。请注意在 foreach($upgrades as $upgrade) 中使用复数和单数 var 名称,这避免了您可能 运行 一路上遇到的名称冲突。 (因此所有大写的 var 名称)