2

模型:

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 行,可能是因为我的 1 per_page 限制。但是,为什么 row_data 是空的?它应该打印行值吗?

另外,即使 row_data 为空,当我输入 pagination create_links() 时,它应该至少显示正确吗?加载我的图书馆有什么问题吗?

4

2 回答 2

0

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

            <?php echo $this->table->generate($records); ?>
于 2013-10-08T09:02:14.483 回答
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;
于 2013-10-08T08:04:15.190 回答