Php 代码点火器:分页+;表库(不工作)

Php 代码点火器:分页+;表库(不工作),php,codeigniter,pagination,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()));

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

这是我的控制器

$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();    
}
它会生成链接,但不会加载下一组记录。上面的代码有任何更正吗


谢谢

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

请更改:

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

并在您的模型中选择支撑器:

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

你应该确保你的限额($this->limit)有好的数据(只有整数)。如果打开了查询字符串,则可以将字符串“&per_page=”传递到模型中。如果是这样,请使用正则表达式或str_replace清除$this->limit。

这是您的错误

$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);

我很确定分页库只是为您生成链接,您可以将偏移量和限制传递给模型的get()函数(或您称之为的任何函数)。确切地说,看看get_,它有您需要的。您能给我一个提示或示例吗?我刚开始接触php程序。我已经按照你的建议更新了我的代码。但是它不会加载下一组记录。请检查分页链接中的“offset”值。你能告诉我记录的数量和“限制”值吗?您在代码中写的“$products=$this->products\u model->get\u products($limit,$uri\u段)”,并不清楚,将“$uri_段”更改为“$offset”
if($this->uri->segment(uri_segment) > 0){

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

            }
            else{
                $offset=0;

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