Search code examples
phpmysqlcodeigniter-3insert-update

Getting error of undefined variable in codeigniter?


I am writing code for displaying data from mysql db to front end. I am using code igniter for this. I am newbie in CI and in oops too. that's why i am not able to find correct way to solve my error.

This is my controller:

<?php
    class Menu_items extends CI_Controller {

     function _Menu_items(){  
      $this->load->model('Menu_items_model');
     }


     public function index(){
      $query = $this->Menu_items_model->get_items();
      $data['EMPLOYEES'] = null;
      if($query)
      {
        $data['EMPLOYEES'] =  $query;
      }
      $this->load->view('layouts/sidebar.php', $data);
     }
    }
?>

My Model:

<?php
class Menu_items_model extends CI_Model { 

 function get_items(){
  $this->db->select("MENU_ID,MENU_DISPLAY");
  $this->db->from('app_menu_items');
  $query = $this->db->get();
  return $query ->result(); 
 }
}
?>

my view where i got this error:

<?php
    if (is_array($result['EMPLOYEES'])) 
      {
       foreach ($result as $row) 
      {?>
         <li><?php echo $row->MENU_ID;?></li>
         <li><?php echo $row->MENU_DISPLAY;?></li> 
      }
     <?php  } ?>
       else
        {
       echo 'record not found';
     <?php  } ?>

Solution

  • Your view is checking for the wrong variable.

    Try:

    if (isset($EMPLOYEES) && is_array($EMPLOYEES))
    {
        foreach ($EMPLOYEES as $row)