doumiang2297 2015-03-14 04:51
浏览 122
已采纳

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

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>
  • 写回答

1条回答 默认 最新

  • dongyejun1983 2015-03-14 06:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 软件定义网络mininet和onos控制器问题
  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。