开发者

codeigniter: pagination + table libraries (not working)

I need help on my code igniter app..i can't seem to find out what's causing the pagination not to work

here's my controller

$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-开发者_JAVA技巧>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);

here's my model

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

it generates the links but then it doesn't load the next set of records..any corrections on the code above?

thanks!


You should give the page to the table, not the full table. Pagination just fills the links, but doesn't make any change on your data, it's your responsability.

Please change:

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

with:

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

and in your model make the propper selection:

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


You should make sure that your limit($this->limit) has good data (only integers). If you have query string turned on you may get the string '&per_page=' passed onto your model. If so clean $this->limit with a regular expression or str_replace.


Here is your error

$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.

So click on per link you have to change the offset value to show the data. try this

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

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

            }
            else{
                $offset=0;

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜