CodeIgniter Forums
Pagination jumping by per_page value? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Pagination jumping by per_page value? (/thread-54129.html)



Pagination jumping by per_page value? - El Forum - 08-23-2012

[eluser]papasnorkle[/eluser]
Hi, I'm trying to whip up some simple pagination for a gallery of photos, but I've come up against a bit of a brainblock. I'm newish to codeigniter and I really can't code all that well to begin with, so apologies for any nonsense that I output.

My pagination seems to work in the sense that if I physically type in the address
www.domain.com/home/page/ - I get the first 16 results
www.domain.com/home/page/1/ - I get the next 16 results
www.domain.com/home/page/2/ - I get the next 16 results and so on

However, when I click 'Next' in my pagination I get directed to
www.domain.com/home/page/16/ then
www.domain.com/home/page/32/ and so on

I believe the pagination is getting this number either from the per_page value, but that's just a guess. I'm completely out of my league.

I'm thinking that all I need to do is kindly ask the next button to point to the next page rather than page 16 and we'd be good, but I can't seem to work out why it's not complying with me.

Here's some code that I feel might be relevant to the issue, apologies if it seems disjointed, it's a work in progress.

Code:
function get_pagination_config($type,$extra1="",$extra2="")
{
  $config = array();
  $config['base_url'] = base_url().$type.'/page';
  $config['use_page_numbers'] = TRUE;
  $config['display_pages'] = FALSE;
  $config['next_link'] = ' Next >';
  $config['prev_link'] = '< Previous ';
  $config['last_link'] = FALSE;
  $config['first_link'] = FALSE;
  switch($type)
  {
   case 'home':
    $config['uri_segment'] = 3;
    $config['total_rows'] = $this->get_total_rows($rows);
    $config['per_page'] = 16;
   break;
  }
  return $config;
}

Code:
function get_photomagraphs($pid="", $pagenum="", &$photomagraphs_check)
{
  $this->db->select('*');
  $this->db->from('photomagraphs');
  $this->db->order_by("photo_id","desc");
  
  if( $pid!="" && is_numeric( $pid ) && ( $pagenum=="view" || $pagenum=="" ) )
  {
   $this->db->where('photo_id',$pid);
  }
  $this->db->order_by("photo_id", "desc");
  if( $pid=="page" && ( $pagenum>0 && $pagenum!="" && is_numeric( $pagenum ) ) )
  {
   $this->db->limit(16, (16*($pagenum)-1)+2);
   //$this->db->limit($this->per_page, (($pagenum)-1 * $this->per_page) + $offset);
  }
  else
  {
   $this->db->limit(16);
  }
  
  $query = $this->db->get();
    
  $result_array = $query->result_array();

  $this->db->flush_cache();

  $photo = reset( $result_array );

  if( in_array($photo['photo_id'],$photomagraph_check) )
  {
   $result_array = $this->get_photomagraph($photomagraph_check);
  }
  else
  {
   $photomagraph_check[] = $photo['photo_id'];
  }
  
  return $result_array;
}

Your help is hugely appreciated. Thanks.


Pagination jumping by per_page value? - El Forum - 08-23-2012

[eluser]Aken[/eluser]
Post the code where you actually initialize and use the Pagination library. Your pagination config looks fine for using page numbers the way you want, so it's probably with how you're initializing the code. Smile


Pagination jumping by per_page value? - El Forum - 08-23-2012

[eluser]papasnorkle[/eluser]
[quote author="Aken" date="1345768615"]Post the code where you actually initialize and use the Pagination library. Your pagination config looks fine for using page numbers the way you want, so it's probably with how you're initializing the code. Smile[/quote]

Thanks for the swift response Aken, I really appreciate your help, see below for the additional code.

taken from the controller:
Code:
public function type($type = "home", $identifier1 = "", $identifier2 = "")
{
  $this->load->spark('ci_alerts/1.1.7');
  $this->load->spark('ci_authentication/1.3.4');
  $this->load->library('pagination');
  $config = $this->Main_model->get_pagination_config($type, $identifier1, $identifier2);
  if( !empty($config) )
  {
   $this->pagination->initialize($config);
  }
  $data = array();
  $data['identifier1'] = $identifier1;
  $data['identifier2'] = $identifier2;
  $data['page_name'] = $type;
  $data['apps'] = $this->Main_model->get_main_layout($type,$identifier1,$identifier2);
  $data['app_list'] = $this->Main_model->get_app_list($type);
  if($data['apps']!="error")
  {
   $data['head'] = "template/html_top";
   $this->load->view('default_view',$data);
  } else {
   $data = array();
   $data['error_code'] = 404;
   $this->load->view('error_view',$data);
  }
}

taken from the view:
Code:
<div class="pagination">
&lt;?php echo $this->pagination->create_links(); ?&gt;
</div>



Pagination jumping by per_page value? - El Forum - 08-23-2012

[eluser]Aken[/eluser]
I still don't see anything that jumps out as wrong. What version of CodeIgniter are you using? The page number option is relatively new - maybe check the Pagination class to see if it is actually an option (it'll be in the list of vars at the top).

If that's good, some other things to check:

- Your $config array actually looks like it should when it reaches the type() controller method.
- You aren't loading another pagination before/after mistakenly.

And unrelated things that I noticed:

- Use site_url() for your website's page URLs. base_url() is for static assets and stuff that doesn't go through index.php
- Inside your get_pagination_config() method, when you call $this->get_total_rows(), you're including the $rows variable which doesn't exist in that scope.


Pagination jumping by per_page value? - El Forum - 08-24-2012

[eluser]papasnorkle[/eluser]
[quote author="Aken" date="1345790141"]I still don't see anything that jumps out as wrong. What version of CodeIgniter are you using? The page number option is relatively new - maybe check the Pagination class to see if it is actually an option (it'll be in the list of vars at the top).

If that's good, some other things to check:

- Your $config array actually looks like it should when it reaches the type() controller method.
- You aren't loading another pagination before/after mistakenly.

And unrelated things that I noticed:

- Use site_url() for your website's page URLs. base_url() is for static assets and stuff that doesn't go through index.php
- Inside your get_pagination_config() method, when you call $this->get_total_rows(), you're including the $rows variable which doesn't exist in that scope.[/quote]

Dude, you're a genius! I was using CodeIgniter 2.0.2 and as suspected the page number option wasn't available. After upgrading to 2.1.2 it's working perfectly. Your unrelated notes are also most helpful, I shall rectify. Thanks again Aken, I would've never caught that on my own. A fictional bucket of handclaps has been dispatched. May your day be splendiferous!