使用codeigniter进行分页

时间:2012-02-11 19:24:32

标签: php codeigniter

我对codeigniter的分页类有一点问题 这是我的控制器

function index()
    {
        $this->load->library('pagination');
        $config['base_url'] = base_url().'index.php/books/index/';
        $config['total_rows'] = $this->db->count_all('books');
        $config['per_page'] = '5';
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        $this->pagination->initialize($config);
        echo "Here";
        //load the model and get results
        $this->load->model('books_model');
        $data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3));

        // load the HTML Table Class
        $this->load->library('table');
        $this->table->set_heading('Title');

        // load the view
        $this->load->view('books_view', $data);
    }

这是视图

<body>
<h1>Books</h1>
<?php echo $this->table->generate($results); ?>
<?php echo $this->pagination->create_links(); ?>
</body>

和模型

function get_books($num, $offset) {
    $query = $this->db->get('books', $num, $offset);    
    return $query;
  }

我已经设置了我的数据库和路由(默认 - >书籍),但页面没有显示任何内容。我的数据库表是书籍。

1 个答案:

答案 0 :(得分:0)

我不知道$config是否是您自己班级的变量。如果是全局配置,也许您应该使用$this->config->set_item函数设置其参数。

此外,为了避免像之前提到的@Henesnarfel这样的麻烦,尝试在执行查询时回显结果的数量。

function index()
{
    $this->load->library('pagination');
    $this->config->set_item('base_url',base_url().'index.php/books/index/');
    $this->config->set_item('total_rows',$this->db->count_all('books'));

    // ECHO to make me sure that the query is succesfully done
    echo("Found ".$this->db->count_all('books')." books");

    $this->config->set_item('per_page','5');
    $this->config->set_item('full_tag_open','<p>');
    $this->config->set_item('full_tag_close','</p>');
    $this->pagination->initialize($config);
    echo "Here";

    //load the model and get results
    $this->load->model('books_model');
    $data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3));

    // load the HTML Table Class
    $this->load->library('table');
    $this->table->set_heading('Title');

    // load the view
    $this->load->view('books_view', $data);
}