0

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

1
  • 3
    The problem is how you load the views, it should be done like this instead: $this->load->view('employee', $data); Commented Nov 8, 2019 at 10:34

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

controller

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

1 Comment

Please describe in your answer, what was the problem, and how will this snippet solve it, to help others understand this answer
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.