无法从数据库向表中显示数据

时间:2018-01-20 16:31:13

标签: php mysql codeigniter

这是我面临的一种错误:

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

这是我的instructor.php代码行:

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

我的控制器:

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

我的模特:

class instructor_model extends CI_Model {

function fetch_data(){

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

    }
}

1 个答案:

答案 0 :(得分:0)

更改您的控制器代码,如下所示。它会工作。 控制器代码

static void createBooks() {
    Book [] inventory = new Book[maxBooks];
    System.out.println("How many books would you like to add?");
    int newBooks = keyboard.nextInt();
    int createBooks;
    if(newBooks>(maxBooks-findNumberOfCreatedBooks())) {
        System.out.println("Sorry, your bookstore has a capacity of " + maxBooks + ", and you have " + findNumberOfCreatedBooks() + " books already created.");
        System.out.println("Therefore we will add a maximum of " + (maxBooks-findNumberOfCreatedBooks()) + " to your collection.");
        createBooks = (maxBooks-findNumberOfCreatedBooks());

    }
    else
        createBooks=newBooks;

    for(int x =findNumberOfCreatedBooks(); x<createBooks;x++) { //creates a new book where one doesn't exist
        System.out.println("***Book"+x+"***");
        System.out.println("Please enter the name this book:");
        String name = keyboard.next();

        System.out.println("Please enter the author of this book:");
        String author = keyboard.next();

        System.out.println("Please enter the ISBN of this book:");
        keyboard.nextLine();
        long isbn = keyboard.nextLong();

        System.out.println("Please enter the price of this book:");
        double price = keyboard.nextDouble();

        inventory[x] = new Book(name,author,isbn,price);
    }

型号代码: -

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

查看代码: -

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