如何从控制器返回查询中的数据

如何从控制器返回查询中的数据

问题描述:

I have a question regarding on how I will retrieve the data that I query in my model to my controller and pass it in my view.

Controller:

class View_book_controller extends CI_Controller {

function __construct() {

    parent::__construct();
    $this->load->helper('cookie');
    $this->load->helper('url');
    $this->load->library('session');
}

function loadView() {
    $this->load->model('view_book_model');
    $postlist['studentID'] = $this->input->get_post('studentID');

    $this->view_book_model->getBookList();

    $this->load->view('viewbooks.php', $postlist);
 }
}

Model:

class View_book_model extends CI_Model {

function getBookList() {

    $query = $this->db->query("SELECT * from books");

    if($query->num_rows() > 0 ) {
        return true;
    }
    else {
        return false;
    }
}
}

and here is the view for it. I want to put the data in a table and view it like a list.

 <!DOCTYPE HTML>
 <html>

 <head>
 <meta charset='UTF-8'>

<link rel="stylesheet" href="<?=base_url(); ?>/public/css/tables.css"/>

<script src="<?=base_url()?>/public/js/jquery.js"></script>
 </head>

<body>
    <input type="hidden" value = "<?php echo $studentID; ?>"/>
    <h3> Search books </h3>
    <input type="text"> </input>
    <input type="submit" value="Search"> </input>
    <br><br>

    <?php
        echo '<hr>';
        echo '<h3> Book List </h3>';
        echo '<table id="maintable"  class="table">';

            echo '<th> Book ID</th>';
            echo '<th> Book Title </th>';
            echo '<th> Book Author </th>';
            echo '<th> Quantity </th>';
            echo '<th> On-hand </th>';

            echo '<tr>
                    <td>1</td>
                    <td>An Abundance of Katherine</td>
                    <td>John Green</td>
                    <td>10</td>
                    <td>5</td>
                 </tr>';
        echo '</table>';
    ?>
</body>
 </html>

model

First, make sure your model returns row. On your code you just return boolean instead of array|object of records. Therefore, you can do it like this:

Check how to generate result in codeigniter.

class View_book_model extends CI_Model {
    function getBookList() {
        $query = $this->db->query("SELECT * from books");

        return $query->result();
    }
}

controller

On your controller, store the model records on $postlist array, in my case, I put it as book_list for example, and pass it to your view loader.

class View_book_controller extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->helper('cookie');
        $this->load->helper('url');
        $this->load->library('session');
    }

    function loadView() {
        $this->load->model('view_book_model');

        $postlist['studentID'] = $this->input->get_post('studentID');
        $postlist['book_list'] = $this->view_book_model->getBookList();

        $this->load->view('viewbooks.php', $postlist);
     }
}

view

In your view you can check the rows using the $postlist key as the variable name:

<? var_dump($book_list); ?>

You can generate table either manually using foreach print the HTML or using the built in HTML Table Class of codeigniter.