Jump to content

How do you limit pagination links/numbers?


mospeed

Recommended Posts

The pagination script that I have has been working great but now that I have over 20 pages it looks like this:

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Next

 

whereas I'd like to limit it to look something like this:

 

1 2 3 4 5 6 7 8 9 ... 19 20 Next

 

What do I need to add to it to do this? For a refence, this is my pagination file here:

 

$display = 9;
$pages = ceil($num/$display);

$current_page = $id;

if ($current_page != 1) {
echo '<a href="index.php?p=' . ($id - 1) . '">Previous</a> ';

}

for ($i = 1; $i <= $pages; $i++) {
if ($i != $current_page) {
	echo '<a href="index.php?p=' . (($pages * ($i / $pages))) . '">' . $i . '</a> ';
} else {
	echo $i . ' ';
}		
} // End for FOR loop.

// If it's not the last page, make a next button:
if ($current_page != $pages) {
echo '<a href="index.php?p=' . ($id + 1) . '"> Next</a>';

}

Link to comment
Share on other sites

My pagination shows however many pages on either side of the current page that you set as $range.

 

For example, my range is set to 5, if I am on page 10, my link would look like this:

 

<<  < [5][6][7][8][9] 10 [11][12][13][14][15] >  >>

 

Here's the code, though you may have to edit it a bit to suit your needs, or you can take from it and use on your own as you like :)

 

$rowsperpage = 10; // how many items per page
$range = 10;// how many pages to show in page link

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))
{

   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
}
else
{
   // default page num
   $currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;

$sql = "SELECT COUNT(*) FROM "BLA BLA BLA";
$result = mysql_query($sql, $db) or die(mysql_error());
$r = mysql_fetch_row($result);
$numrows = $r[0];
$totalpages = ceil($numrows / $rowsperpage);

if ($currentpage > $totalpages) 
{
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) 
{
   // set current page to first page
   $currentpage = 1;
} // end if

PUT YOUR SQL CODE HERE AS WELL AS EVERYTHING THAT SHOULD BE ON THE PAGE.
THE FOLLOWING GOES BELOW AT THE BOTTOM OF YOUR CONTENT:

$result = mysql_query("SELECT * FROM BLA BLA BLA, $db);
$num_rows = mysql_num_rows($result);
if ($num_rows<1)
	{
?>
 
<?php
}
ELSE {
echo "Page ".$currentpage." of ".$totalpages."<br>";
if ($currentpage > 1) 
	{
   // show << link to go back to page 1
   echo " <a href='YOURPAGE.php?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='YOURPAGE.php?currentpage=$prevpage'><</a> ";
	} // end if

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++)
		{
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages))
   				{
      // if we're on current page...
      if ($x == $currentpage)
  				{
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
				}
else
				{
         // make it a link
         echo " <a href='YOURPAGE.php?currentpage=$x'>$x</a> ";
				 } // end else
			} // end if
		} // end for

// if not on last page, show forward and last page links
if ($currentpage != $totalpages)
		{
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page

   echo " <a href='YOURPAGE.php?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='YOURPAGE.php?currentpage=$totalpages'>>></a> ";
		} // end if
	} // end else
}


 

So, in this, simply change the BLA BLA BLA to your SQL queries, and YOURPAGE.php to whatever page you're using the code on.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.