Michan Posted November 16, 2006 Share Posted November 16, 2006 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> ');else echo ('<img src="gfx/i_prev_off.gif"> ');for ($i = 0; $i <= ($pages - 1); $i++) echo (' <a href="article_author.php?author='.$author.'&page='.$i.'">'.($i + 1).'</a>');if ($pages > ($page + 1)) echo (' <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 More sharing options...
JasonLewis Posted November 16, 2006 Share Posted November 16, 2006 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> ');}else{ echo ('<img src="gfx/i_prev_off.gif"> ');}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 pagesif($i == $page){ //this checks if the current page is equal to i. if it is we will unlink it. echo (' ['.$i.']');}else{ //nope, lets link it. echo (' <a href="article_author.php?author='.$author.'&page='.$i.'">'.$i.'</a>');}}}if ($pages > ($page + 1)){ echo (' <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. Link to comment https://forums.phpfreaks.com/topic/27418-pagination-with-for-gah/#findComment-125410 Share on other sites More sharing options...
Psycho Posted November 16, 2006 Share Posted November 16, 2006 There are pagination tutorials right on this site:http://www.phpfreaks.com/tutorial_cat/25/Page-Number--Pagination.phpI 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]<?phpfor ($pageNum = $page-3; $pageNum<=$page+3; $pageNum++) { if ($pageNum>0 && $pageNum<=$pages) { if ($pageLinks != "") { $pageLinks .= " | "; } if ($pageNum!=$page) { $pageLinks .= "<a href="articles_author.php?author=".$author."&page=".$pageNum."\">$pageNum</a>"; } else { $pageLinks .= $pageNum; } }}echo $pageLinks;?>[/code] Link to comment https://forums.phpfreaks.com/topic/27418-pagination-with-for-gah/#findComment-125416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.