pagination: display a specific number page links

Alin
2008-04-30 18:37:33
pagination: display a specific number page links
Hello.
I've been trying to customize a pagination system I already use, to display only a specific number of links for the next pages, (google style) depending on the page where the user is know.

For example, if I have about 200 pages, it won't look good displaying all 200 links, so I tryed to show something like:
Page: 1 ... 100, 101, 102 ... 200
So the first and last pages will always be there, just the ones in the middle will change when the user clicks the next page. (The middle one, 101 is where the user should be).

The problem is know if the user is on page 1 or 2, or the last page. I'm not sure how to set the page to display only first 3 pages, and the last page, if the user is in page 1 (for example). Here is what I did:

if(isset($_GET['page'])) { $page = $_GET['page']; } else { $page=1; }
$start = ($page-1)*6;
$sql = mysql_query("SELECT COUNT(id) FROM `table`");
$row = mysql_fetch_row($sql);
$records = $row[0];
$pages = ceil($records / 6);
echo '<center>Page: ';
for($i=$page-1; $i<3 $i++) {
echo '<a href="address.php?page='.$i.'">'.$i.'</a>, ';
}
echo '</center>';
This shows the 3 pages, the one in the left is the previeous, in the middle is the current one, and the next one. But like I said, the problem is if the user is in page 1, or in the last page.
I know I can check with if($page==1)...
This is how I started, but I got stucked when I realized that I must check if the user is in page 1, or if is in page 2 (case when I display Page: 1, 2, 3 ... last) or (if there are 200 pages) if the user is in the last page, I display Page: 1 ... 198, 199, 200.
But then I also thought "What if there are only 2 or 3 pages, not 200 ?" I need to verify all this or maybe there is a simple way ? I'm not sure how should I do this.

Any ideas ?
Thanks,
Alin.
Veselin
2008-05-01 10:16:47
Re:
this is a sample code which will make google style pagination links. Just save it as pagination.php file and test it

<?php
$totalPages = 200;
$showPages = 5;
if ($_REQUEST["page"]>0) { $currentPage = $_REQUEST["page"]; } else { $currentPage=1; };

$startPage = $currentPage - ($showPages-1) / 2;
if ($startPage<1) {
$endPage = $showPages;
$startPage = 1;
} else {
$endPage = $startPage + $showPages - 1;
};

if ($currentPage>($showPages-1)/2+1) {
echo "<a href='pagination.php?page=1'>1</a> ";
echo " ... ";
$startCount = 2;
} else {
$startCount = 1;
};

if ($currentPage<($totalPages-($showPages-1)/2)) {
$endCount = $totalPages-1;
} else {
$endCount = $totalPages;
};

if ($endPage>$totalPages) {
$startPage = $totalPages - $showPages + 1;
};

for ($i=$startCount; $i<=$endCount; $i++) {

if ($i>=$startPage && $i<=$endPage) {

if ($i==$currentPage) {
echo $i." ";
} else {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
};

};

};
if ($endCount<$totalPages) {
echo " ... ";
echo "<a href='pagination.php?page=".$totalPages."'>".$totalPages."</a> ";
};
?>
Alin
2008-05-03 02:06:08
Re:
It works great. The way I thought about doing the code was completely different; I wouldn't got it working.

Thanks a lot.
Reply
Title:
Your name:
Your email: (email address will not be posted on the web site)
Reply:
Verification