Jump to content

Pagination with for(), gah!


Michan

Recommended Posts

Hi,

I'm trying to tackle pagination, but I'm stumped.

I've created the below code to do the following:
[list]
[*]If there page has less than 100 results, do not paginate
[*]Display linked/unliked prev/next buttons depending on current result page
[*]Show page numbers from 1 - X (where X = last page number)
[/list]

The below code works [b]fine so far[/b] (page 0 is the first page, but that doesn't matter, as the code considers this), but I'd like to do more which I can't figure out myself, and I can't find a relevant tutorial.

I'd like it to display the previous three pages and next three pages, with an UNLINKED current page in the middle.
Example (if there are 10 pages, and you're on page 5): [b]2 | 3 | 4 [ 5 ] 6 | 7 | 8[/b]

[code]// If there are over 100 articles, we'll need to do something about it D:

$prev = ($page - 1);

$next = ($page + 1);

$pages = ceil($num_rows / $limit);

if ($page > 0)
echo ('<a href="articles_author.php?author='.$author.'&page='.$prev.'"><img src="gfx/i_prev.gif" border="0"></a>&nbsp;');

else
echo ('<img src="gfx/i_prev_off.gif">&nbsp;&nbsp;');

for ($i = 0; $i <= ($pages - 1); $i++)
echo ('&nbsp;<a href="article_author.php?author='.$author.'&page='.$i.'">'.($i + 1).'</a>');

if ($pages > ($page + 1))
echo ('&nbsp;&nbsp;<a href="articles_author.php?author='.$author.'&page='.$next.'"><img src="gfx/i_next.gif" border="0"></a></div><BR><BR><BR></span>');

else
echo ('<img src="gfx/i_next_off.gif"></div><BR><BR><BR></span>');[/code]

Many thanks in advance to ANYONE who can help with this!

- Mi
Link to comment
https://forums.phpfreaks.com/topic/27418-pagination-with-for-gah/
Share on other sites

well.. for a start just make $i equal to 1. saves a bit of fuss. then. look a this code:

[code]
// If there are over 100 articles, we'll need to do something about it D:

$prev = ($page - 1);

$next = ($page + 1);

$show = 3; //this variable will show 3 numbers either side of the current page.

$pages = ceil($num_rows / $limit);

if ($page > 0){
echo ('<a href="articles_author.php?author='.$author.'&page='.$prev.'"><img src="gfx/i_prev.gif" border="0"></a>&nbsp;');
}else{
echo ('<img src="gfx/i_prev_off.gif">&nbsp;&nbsp;');
}

for ($i = ($page - $show); $i <= ($page + $show); $i++){
if($i > 0 && $i <= $pages){ //this makes sure we arn't going into negatives and going over the total pages

if($i == $page){ //this checks if the current page is equal to i. if it is we will unlink it.
echo ('&nbsp;['.$i.']');
}else{ //nope, lets link it.
echo ('&nbsp;<a href="article_author.php?author='.$author.'&page='.$i.'">'.$i.'</a>');
}
}
}

if ($pages > ($page + 1)){
echo ('&nbsp;&nbsp;<a href="articles_author.php?author='.$author.'&page='.$next.'"><img src="gfx/i_next.gif" border="0"></a></div><BR><BR><BR></span>');

}else{
echo ('<img src="gfx/i_next_off.gif"></div><BR><BR><BR></span>');
}
[/code]

there you are. i think that should work. unless i mistyped something.
There are pagination tutorials right on this site:
http://www.phpfreaks.com/tutorial_cat/25/Page-Number--Pagination.php

I was working on this when the other guy posted. The only thing this does differently is add logic to add the pipe (|) between the page numbers when appropriate.
[code]<?php
for ($pageNum = $page-3; $pageNum<=$page+3; $pageNum++) {
 if ($pageNum>0 && $pageNum<=$pages) {
   if ($pageLinks != "") { $pageLinks .=  "&nbsp;|&nbsp;"; }
   if ($pageNum!=$page) {
     $pageLinks .= "<a href="articles_author.php?author=".$author."&page=".$pageNum."\">$pageNum</a>";
   } else {
     $pageLinks .= $pageNum;
   }
 }
}
echo $pageLinks;
?>[/code]

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.