1

i have made codeigniter pagination to show my product in image. but something is wrong on the second page, it is not sustainable or continuously.

i set per_page is: 6, my total data is 11.

in page 1 result is ok, show image by id form 1 - 6,but when i click second page the start from 3 - 8.

this is the 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 is my view

<?php
foreach($results as $data) { ?>
<div class="col-lg-3 text-center">
<a href="<?php echo base_url();?>item/subitem/<?php echo $data->id; ?>"> <!-- to 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 this the controller look 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');
 }
Bobby Z
  • 765
  • 1
  • 9
  • 21

1 Answers1

0

You need to change your second parameter in controller, means $page variable value:

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$pageLimit = ($page*$config['per_page']); //Add this line

After that pass the $pageLimit variable in

$data['results'] = $this->item_m->fetch_jenislogam($config['per_page'],  $pageLimit);

Reason: limit function's second parameter is be offset. And in your case, it is set 1,2,3 ...

FatalError
  • 560
  • 2
  • 18
  • thanks it work only at the first time when i click the menu `Item` show item 1 - 6, but when i click second page, it got error `Severity: Warning Message: Invalid argument supplied for foreach()` @fatalerror – Bobby Z May 16 '15 at 14:35
  • i have changed model code to clear the error.but, still got wrong content placement. when the first time i access the page from header link menu `base_url?>controller/method` load 1 - 4 content, but when i click number 2 it showed 9 to 11 content. echo result echo `$this->db->last_query();exit();` SELECT * FROM 'mytable' LIMIT 4` //when the first time the page load,and this when i click the second page `SELECT * FROM `mytable` LIMIT 7, 4 ` i can't click first link. @fatalerror – Bobby Z May 16 '15 at 21:16
  • i've found the solution from this thread. [link](http://stackoverflow.com/questions/9046149/pagination-do-not-correct-display-page-numbers-codeigniter). just add this code `$start = $per_page * ($start_from-1);` – Bobby Z May 17 '15 at 15:08