简体   繁体   中英

CodeIgniter PHP Undefined Variable error

I've been struggling for the last 2 hours to find out why i'm getting this error, but i'm still stuck! I've checked the answers on here but still nothing.

After checking a similar thread I can't seem to find a solution

help would be appreciated

error message:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: UPGRADES

Filename: views/Test.php

Line Number: 207

Model:

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

Controller:

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

}
?>

View:

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

Fix your model code to:

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

Model:

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

If rows were found this will return the results as an object and return NULL if there were no rows. Changed to returning an object instead of an array because that's what type of usage you have in the View with the $upgrade->key syntax. To use an array you would use $upgrade['key']

In the controller use this

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

New View:

<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>

Notice $UPGRADES was changed to $upgrades. Convention is that all upper case names indicate a constant. Note the uses of plural and singular var names in foreach($upgrades as $upgrade) which avoids the name collision you probably ran into along the way. (and thus the all caps var name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM