homepage Welcome to WebmasterWorld Guest from 54.166.95.146
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Pagination help
pagination
chrissim




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

hi,

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


 

Matthew1980




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

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 ]


Cheers,
MRb

enigma1




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

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.

chrissim




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

Thanks guy i give it a try here :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved