Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Pagination help




4:39 am on Jul 13, 2010 (gmt 0)

5+ Year Member


How do you fix the below pagination to show first 5 or 10 pages? something like previous 1,2,3,4,5 next. The pagination at the moment is showing all pages and it was kind of messy.

$count_result = mysql_query("SELECT COUNT(*) FROM $table");
$count_row = mysql_fetch_array($count_result);
//fetch the total number of rows in the table
$count = $count_row["COUNT(*)"];
//we will use this information to build our navigation list

echo (($page>1)?"<a href='".build_url("sql.php", "page", $page-1)."'><font size='2px'>previous</font></a>":"previous")." | ";
for($i=1; $i<=($count/$records_per_page)+1; $i++){
echo "<a href='".build_url("sql.php", "page", $i)."'>$i</a>";
echo $i;
//output a little symbol (|) to seperate the links
//but not for the last link
echo " | ";
echo " | ".(($page<$count/$records_per_page)?"<a href='".build_url("sql.php", "page", $page+1)."'><font size='2px'>next page</font></a>":"next");


9:18 am on Jul 13, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

Hi there chrissim,

Whether this is relevant or not, I think you would benefit from reading this, Eelixduppy posted this years ago (and much kudos to him for it :)), and it helped me to understand the mechanics on pagination, and rather than re post whats been said & discussed, read this and all will become clear.

Enjoy: [webmasterworld.com ]



11:41 am on Jul 13, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

You need to modify your for loop so the page numbers displayed are subject to the page viewed.

$total_pages = ceil($count/$records_per_page);
$cur = (int)($page / $count);
for($i=1+(($cur-1)*$count); ($i<$cur*count) && $i<=$total_pages; $i++){

And you likely need to expand the mysql query to take into account not just tables, see how the oscommerce does it, where this code sample is based on.


2:38 pm on Jul 13, 2010 (gmt 0)

5+ Year Member

Thanks guy i give it a try here :)

Featured Threads

Hot Threads This Week

Hot Threads This Month