Jump to content

Pagingation Not Flicking Through Pages


justlukeyou

Recommended Posts

Hi,

 

I have done some pagination code which echos 15 results onto a page and echos the number of pages complete with a link.  Hovever, when I click on a link nothing is happening,  it just reloads the page starting from 1.    Can anyone advise where I am going wrong please?

 

<?php

$per_page = 15;

$pages_query = mysql_query("SELECT COUNT(`description`) FROM `productdbase`");
echo $pages = mysql_result($pages_query, 0);
echo $pages = ceil(mysql_result($pages_query, 0) / $per_page);

$page = (isset($_GET['page'])) ?  (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;

$query = mysql_query("SELECT `description` FROM `productdbase` LIMIT $start, $per_page");
while ($query_row = mysql_fetch_assoc($query)) {

echo '<p>', $query_row['description'] ,'</p>';



}

if ($pages >= 1 && $page <= $pages) {
for ($x=1; $x<=$pages; $x++) {
echo ($x == $page) ? '<strong><a href="?products='.$x.'">'.$x.'</a></strong> ' : '<a href="?products='.$x.'">'.$x.'</a> ';
}
}


?>

Link to comment
https://forums.phpfreaks.com/topic/262843-pagingation-not-flicking-through-pages/
Share on other sites

First place I'd look for a problem is here:

$page = (isset($_GET['page'])) ?  (int)$_GET['page'] : 1;

 

How are you passing 'page' to the PHP script?  Is it possible that something is going wrong and page isn't being passed, so therefore you are always on page 1?

 

Are your 'prev' and 'next' buttons correctly pointing to: myscript.php?page=x

 

Personally, I always prefer to use $_REQUEST['page'], but that is just laziness, as it allows me to use GET or POST variables without code changes.  Not suggesting you use $_REQUEST, just an option...

 

 

As I stated above, your problem is here:

if ($pages >= 1 && $page <= $pages) {
  for ($x=1; $x<=$pages; $x++) {
    echo ($x == $page) ? '<strong><a href="?products='.$x.'">'.$x.'</a></strong> ' : '<a href="?products='.$x.'">'.$x.'</a> ';
   }
}

You are setting your page links as ?products=$x

 

But, your PHP script, on this line:

$page = (isset($_GET['page'])) ?  (int)$_GET['page'] : 1;

is asking for a GET variable called page.  This doesn't exist, so page is always equal to zero.

 

You can either:

Set your href urls to ?page=$x

or

$_GET['products']

How about something like:

if ($pages >= 1 && $page <= $pages) {
  for ($x=1; $x<=$pages; $x++) {
    $link = '<a href="?page='.$x.'">'.$x.'</a>';
         if ($x == $page) echo '<strong>'.$link.'</strong> ';  // Current Page
    else if (($x == 1) && ($page>5)) echo $link.' ... ';  // First page
    else if (($x == $pages) && ($page<$pages-5)) echo ' ... '.$link;  // Last Page
    else if ($x >= ($page-5)) && ($x <= ($page+5)) echo $link.' '; // Within 5 pages
   }
}

 

So, if you are on page 12 of 482, it would display:

1 ... 7 8 9 10 11 12 13 14 15 16 17 ... 482

and page 3 should display like:

1 2 3 4 5 6 7 8 ... 482

Okay, well ...

 

<?php

// if you are on page less than 10
$page = filter_input(INPUT_GET, 'page');
$start = $page - 5;
$end = $page + 5;
if ($page < 10)
{
      $start = 1;
      $end = 8;
}

$max_pages = <some php code here to get 482>;

if ($page < 10)
{
    // for loop to print $start -> $end, then 3 dots and $max_pages.
}
else
{
    // for loop to print 1, then 3 dots, then $start -> $end, then 3 dots, and finally 482
}

 

Similar check for if the user is within 8 pages of 482, so:

1 ... 475, 476, 477, 478, 479, 480, 481, 482.

Brilliant, thanks Jamdog.  The last line returns a  unexpected T_BOOLEAN_AND error but I cant see what it is, it looks fine from what I can see.

 

else if (($x == 1) && ($page>5)) echo $link.' ... ';  // First page
    else if (($x == $pages) && ($page<$pages-5)) echo ' ... '.$link;  // Last Page
else if ($x >= ($page-5)) && ($x <= ($page+5)) echo $link.''; // Within 5 pages

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.