Codeigniter表库不生成任何内容

时间:2013-10-08 06:49:38

标签: php codeigniter pagination

型号:

class Pagination_model extends CI_Model
{
    public function __construct() {
        parent::__construct();
    }

    public function total_rows() {
    return $this->db->count_all('recipe');
    }
}

控制器:

 function browse()
    {
    $this->load->library('pagination');
    $this->load->library('table');

        $config['base_url'] = 'http://localhost/Finals/index.php/login/browse';
        $config['total_rows'] = $this->pagination_model->total_rows();
        $config['per_page'] = 1;
        $config['num_links'] = 10;
        $config['uri_segment'] = 3;


        $this->pagination->initialize($config);

        $data['records'] = $this->db->get('recipe', $config['per_page'], $this->uri->segment(3));
        //echo "<pre>";print_r( $data['records'] );die;
        $this->load->view('browse' , $data);
    }

我的观点:

 <html>
    <head>
    <title>blablabla</title>
    <link href="../../css/default.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <div id="header">
        <div id="logo">
           <h1><a href="#">grr</a></h1>
           <h2>by chief</h2>
        </div>
        <div id="menu">
           <ul>
               <li class="first"><a href="#">Home</a></li>
               <li><a href="#">My Profile</a></li>
               <li><a href="#">My Recipes</a></li>
               <li><a href="#">Browse Recipes</a></li>
               <li><a href="#">Forum</a></li>
           </ul>
        </div>
    </div>
    <div id="content">
        <div id="colOne">
           <div class="post">
              <div class="story">
             <h2>Results:</h2>
                <?php $this->table->generate($records); ?>
                <?php $this->pagination->create_links(); ?>
             </div>
              </div>
           </div>
        </div>
        <div id="footer">
           <p>Copyright &copy; </p>
        </div>
    </body>
    </html>

这是从print_r

返回的内容
 [result_id] => mysqli_result Object
            (
                [current_field] => 0
                [field_count] => 8
                [lengths] => 
                [num_rows] => 1
                [type] => 0
            )

        [result_array] => Array
            (
            )

        [result_object] => Array
            (
            )

        [custom_result_object] => Array
            (
            )

        [current_row] => 0
        [num_rows] => 1
        [row_data] => 

它确实显示1行,因为我的per_page限制为1。但是,为什么row_data为空?它应该打印行值吗?

加上,即使row_data为空,当我输入分页create_links()时,它应该至少显示为对吗?加载我的图书馆有什么问题吗?

2 个答案:

答案 0 :(得分:0)

更改此方法,描述总数以选择所有记录并查找控制器部分中的行数的错误方法,而不应该在模型中完成。:

$config['total_rows'] = $this->db->get('recipe')->num_rows();

要:

$config['total_rows'] = $this->model_name->total_rows();
/*Model Function*/
function total_rows(){
    return $this->db->count_all('table_name'); #will return the no. of rows in integer    
}

在分页中缺少必需的配置:

$config['uri_segment'] = 3;

定义具有绝对路径的CSS,JS文件:

<link href="<?=base_url()?>css/default.css" rel="stylesheet" type="text/css">

最后一个print_r您的控制器中的结果,以确保您正确获取值:

$data['records'] = $this->db->get('recipe', $config['per_page'], $this->uri->segment(3) );
echo "<pre>";print_r( $data['records'] );die;

答案 1 :(得分:0)

Table :: generate创建一个字符串。你需要回应它。

            <?php echo $this->table->generate($records); ?>