codeigniter:分页+表库(不工作)

时间:2011-07-21 02:34:48

标签: php codeigniter pagination

我需要帮助我的代码点火器应用程序..我似乎无法找出导致分页不起作用的原因

这是我的控制器

$limit = 15;
        $uri_segment = 4;
        $this->load->helper('string');

        $offset = $this->uri->segment( $uri_segment );
        log_message('error',date('Y-m-d H:i:s', time()));
        //load
        //$products = $this->Products_model->get_all();
        $products = $this->Products_model->get_products($limit, $uri_segment);
        //var_dump($products);

        log_message('error', $products );

        //pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('admin/products/index/');
        $config['total_rows'] = $this->Products_model->count_all();
        $config['per_page'] = $limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();

        //table
        $this->load->library('table');
        $this->table->set_empty(" ");
        $this->table->set_heading('ID', 'Product','Category', 'Actions');
        $i = 0 + $offset;
        foreach ($products as $product){
            ++$i;
            $this->table->add_row($product->id, $product->title, $this->Categories_model->get_by_id($product->category_id)->title,
                anchor('/admin/products/update/'.$product->id,'update',array('class'=>'update')).' '.
                anchor('/admin/products/delete/'.$product->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this product?')"))
            );
        }
        $data['table'] = $this->table->generate();

        //load template
        $this->products_list();
        $this->products_template();
        $this->template->build('products/productList', $data);

这是我的模特

function get_products($num, $offset) {
    return $this->db->get($this->tbl_products, $num, $offset)->result();    
}

它会生成链接但是它不会加载下一组记录。对上面的代码进行任何更正?

谢谢!

3 个答案:

答案 0 :(得分:1)

您应该将页面提供给表格,而不是完整的表格。分页只是填充链接,但不会对您的数据进行任何更改,这是您的责任。

请更改:

        $products = $this->Products_model->get_all();

使用:

        $products = $this->Products_model->get_page( $Offset, $this->limit ); // It's not clear where do you define limit.

并在您的模型中进行适当选择:

function get_page( $offset, $limit ){
    return $this->db->get( $this->tbl_products, $limit, $offset )->result();
}

答案 1 :(得分:0)

您应该确保您的限制($ this->限制)具有良好的数据(仅限整数)。如果您打开了查询字符串,则可能会将字符串'& per_page ='传递给您的模型。如果这样干净$ this->使用正则表达式或str_replace限制。

答案 2 :(得分:0)

这是您的错误

$products = $this->Products_model->get_products($limit, $uri_segment);

$limit=the number of data you want to show in per page,

$offset=the data number start from.

因此,单击每个链接,您必须更改偏移值以显示数据。试试这个

if($this->uri->segment(uri_segment) > 0){

                $offset=$this->uri->segment($uri_segment);

            }
            else{
                $offset=0;

            }
$products = $this->Products_model->get_products($limit, $offset);