0

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

  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Qirel Oct 19 '15 at 15:02
  • Well i guess the query does not return results. To test that replace `$data['UPGRADES'] = null;` with `$data['UPGRADES'] = array();` - it should work – Steve Oct 19 '15 at 15:04
  • And if you take a look at the variable-names, you use `$upgrades` in the `foreach`-statement, but `$upgrade` inside it. – Qirel Oct 19 '15 at 15:04
  • changed to upgrades and swapped null for array and still getting the error... – matthew royall Oct 19 '15 at 15:07
  • You forgot to enclose constructor method of your model. Also, model code belongs to old (less than 2.x.x) version of CI. Is that supose to be? What is CI version of your app? – Tpojka Oct 19 '15 at 15:51

2 Answers2

0

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();    
        }    
    }
?>
Nere
  • 4,097
  • 5
  • 31
  • 71
  • Do I use the same code that i've been using in my controller and view? I'm also struggling to get the data to show in my view. something you could help with? – matthew royall Oct 20 '15 at 10:46
0

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)

DFriend
  • 8,869
  • 1
  • 13
  • 26
  • That's great, It's worked and i've got know errors. Just need to get the data displayed on the screen. Thanks again for your help! – matthew royall Oct 20 '15 at 08:38