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 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python