0
votes

I have this code where data is being defined as far as I can see, these are the errors I am getting:

PHP Error was encountered

Severity: Notice

Message: Undefined variable: empdata

Filename: views/employee.php

Line Number: 51**

Backtrace:

File: C:\xampp\htdocs\providentfund\application\views\employee.php Line: 51 Function: _error_handler

File: C:\xampp\htdocs\providentfund\application\libraries\BaseController.php Line: 99 Function: view

File: C:\xampp\htdocs\providentfund\application\controllers\Employee.php Line: 19 Function: loadViews

File: C:\xampp\htdocs\providentfund\index.php Line: 315 Function: require_once

controller:

      public function display_all()
      {
          $result=$this->Employee_model->display_all();
          $data['empdata']=$result;
         $this->loadViews("employee", $this->global, NULL);

      }

model:

      function display_all()
      {
      $query=$this->db->query("select * from employetbl");

      return $query->result();
      }

and the view:

    <table id="example1" class="table table-bordered table-striped">
              <thead>
              <tr>
                <th>Rendering engine</th>
                <th>Browser</th>
                <th>Platform(s)</th>
                <th>Engine version</th>
                <th>CSS grade</th>
              </tr>
              </thead>
              <tbody>
              <?php
              $cnt=1;

              foreach($empdata as $rec)
              {
                print_r($empdata);
                die();
              ?>
              <tr>
                <td><?php echo $rec['fullName'];?></td>

              </tr>
              <?php
              // for serial number increment
              $cnt++;
              } ?>

Why do I get Error Message "undefined variable"?

3
The problem is how you load the views, it should be done like this instead: $this->load->view('employee', $data);frogman578

3 Answers

0
votes

I think the problem is in your controller:

public function display_all()
      {
          $result=$this->Employee_model->display_all();
          $data['empdata']=$result;
         $this->loadViews("employee", $this->global, NULL);

      }

You are calling the loadViews with $this->global but your "empdata" is not defined there.

You should try changing this:

$this->loadViews("employee", $this->global, NULL);

To this:

$this->loadViews("employee", $data, NULL);
0
votes

controller

public function display_all(){
     $result=$this->Employee_model->display_all();
     $data['empdata']=$result;
     $this->load->view('employee',$data);
}
0
votes

In controller

public function display_all()
      {
          $result=$this->Employee_model->display_all();
          $data['empdata']=$result;
         $this->loadViews("employee", $data);

      }

In model

function display_all()
      {
      $query=$this->db->query("select * from employetbl");

      $result = array();
        foreach ($query->result_array() as $value) {
            $res['variable_name'] = $value['column_name'];
            array_push($result, $res);
        }
        return $result;
      }