php - Wrong calculation codeigniter pagination -
i have made codeigniter pagination show product in image. wrong on second page, not sustainable or continuously.
i set per_page is: 6, total data 11.
in page 1 result ok, show image id form 1 - 6,but when click second page start 3 - 8.
this model
public function record_count() { return $this->db->count_all('mytable'); } public function fetch_jenislogam($limit, $start) { $start--; if($start<0) { $start=0; } $this->db->limit($limit, $start); $query = $this->db->get("tb_jenislogam"); return $query->result_array(); }
this view
<?php foreach($results $data) { ?> <div class="col-lg-3 text-center"> <a href="<?php echo base_url();?>item/subitem/<?php echo $data->id; ?>"> <!-- direct product subitem --> <img class="img-responsive" src='<?php $img = $data->pics; if ($img) { echo base_url().'uploads/origin/'.$data->pics; } else { echo base_url().'uploads/origin/def-img.png'; } ?>'></a> <br> </div> <div> <?php echo $data->id;?></div> <?php } ?>
and controller like.
public function listitem() { $config = array(); $config['base_url'] = base_url().'item/listitem'; $config['total_rows'] = $this->item_m->record_count(); $config['per_page'] = 6; $config['uri_segment'] = 3; $config['num_links'] = 2; $config['use_page_numbers'] = true; $config['cur_tag_open'] = '<a class="current">'; $config['cur_tag_close'] = '</a>'; $config['next_link'] = '>'; $config['prev_link'] = '<'; $this->pagination->initialize($config); $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; $data['links'] = $this->pagination->create_links(); $data['uri'] = $this->uri->segment(3); $data['results'] = $this->item_m->fetch_jenislogam($config['per_page'], $page); $this->load->view('user_header'); $this->load->view('user/usr_item_view', $data); $this->load->view('user_footer'); }
you need change second parameter in controller, means $page
variable value:
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; $pagelimit = ($page*$config['per_page']); //add line
after pass $pagelimit
variable in
$data['results'] = $this->item_m->fetch_jenislogam($config['per_page'], $pagelimit);
reason: limit
function's second parameter offset. , in case, set 1,2,3 ...
Comments
Post a Comment