简体   繁体   中英

Cannot display data to table from database

This is the kind of error that I'm facing:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: fetch_data
Filename: pages/instructors.php 
Line Number: 126"

Here's my instructor.php line of codes:

          <tr>
            <th>ID</th>
            <th>Last Name</th>
            <th>First Name</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>

        <?php
          if($fetch_data) {

            foreach ($fetch_data->result() as $row) {

       ?>
          <tr>  
           <td><?php echo $row->ins_id; ?></td>
           <td><?php echo $row->lastname; ?></td>
           <td><?php echo $row->firstname; ?></td>
           <td><a class="btn btn-info" href="<?php echo base_url('Edit_faculty'); ?>">Edit</a></td> 

          </tr>  
        <?php
            }
          }
          else
          {
        ?>
          <tr>
            <td colspan="3">No data found</td>
          </tr>

       <?php        
        }
       ?>  

        </tbody>
      </table>

my Controller:

class Add_instructor extends CI_Controller{

    public function instructor(){
        $this->load->model('instructor_model');
        $data["fetch_data"] = $this->instructor_model->fetch_data();
        $this->load->vars($data);
        $this->load->view('templates/header');
        $this->load->view('pages/add_instructor', $data);
        $this->load->view('templates/footer');
    }
}

And my model:

class instructor_model extends CI_Model {

function fetch_data(){

        $query = $this->db->query("SELECT * FROM instructor ORDER BY ins_id DESC");
        return $query;

    }
}

Change your controller code as below. It will work. Controller code

class Add_instructor extends CI_Controller{
    public function instructor(){
        $this->load->model('instructor_model');
        $data["fetch_data"] = $this->instructor_model->fetch_data();
        $this->load->view('templates/header');
        $this->load->view('pages/instructor', $data);
        $this->load->view('templates/footer');
    }
}

Model Code:-

class instructor_model extends CI_Model {
    function fetch_data(){
        $query = $this->db->query("SELECT * FROM instructor ORDER BY ins_id DESC");
    return $query->result();
    }
}

View Code:-

  <tr>
        <th>ID</th>
        <th>Last Name</th>
        <th>First Name</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>

    <?php
      if($fetch_data) {
      foreach($fetch_data as $row) {
     ?>
      <tr>  
       <td><?php echo $row->ins_id; ?></td>
       <td><?php echo $row->lastname; ?></td>
       <td><?php echo $row->firstname; ?></td>
       <td><a class="btn btn-info" href="<?php echo base_url('Edit_faculty'); ?>">Edit</a></td> 

      </tr>  
    <?php
        }
      }
      else
      {
    ?>
      <tr>
        <td colspan="3">No data found</td>
      </tr>

   <?php        
    }
   ?>  

    </tbody>
  </table>

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