Nightasy Posted July 21, 2013 Share Posted July 21, 2013 Greetings all, So, I'm working on my page navigation and I'm having a little trouble with my math here. It's probably a brain freeze from too much coding in one day but anyhow. What I'm trying to get my code to do is always show 9 pages and never show anymore then 9 pages in the navigation. Unfortunately that's not the result that I am getting with this script. // List page selectors. for($j = max(1, $current_page - ; $j <= min($current_page + 8, $total_pages); $j++) { $p = $j; echo "<a href='?p=" . $p . "'>" . $p . "</a> "; } I have a different script for the first page, previous page, next page and last page links. They all work fine of course since those are super easy scripts. Any help would be appreciated. Best Regards, Nightasy Link to comment https://forums.phpfreaks.com/topic/280379-pagination-page-display/ Share on other sites More sharing options...
mac_gyver Posted July 22, 2013 Share Posted July 22, 2013 // calculate current_page +/- 4, limited by 1/total_pages $start = max(1, $current_page - 4); $end = min($current_page + 4, $total_pages); // if start resulted in 1, make end = start + 8 if($start == 1){$end = $start + 8;} // if end resulted in total_pages, make start = $end - 8 if($end == $total_pages){$start = $end - 8;} // limit start/end to 1/total_pages $start = max(1, $start); $end = min($end, $total_pages); for($p = $start; $p <= $end; $p++) { if($p == $current_page){ echo "[$p] "; // current_page not a link } else { echo "<a href='?p=$p'>$p</a> "; } } Link to comment https://forums.phpfreaks.com/topic/280379-pagination-page-display/#findComment-1441620 Share on other sites More sharing options...
Nightasy Posted July 22, 2013 Author Share Posted July 22, 2013 @mac_gyver : YES, this makes so much sense. Thank you very much. I actually had to walk away from my computer trying to figure this out. It was getting frustrating. Now that I see your code it's so obvious. Much appreciated!!! Edit: Oh, I kind of got rid of this little bit here: if($p == $current_page){ echo "[$p] "; // current_page not a link } else { It was throwing my divs all out of wack lol. This works great though and is exactly what I wanted. Thank you again! Link to comment https://forums.phpfreaks.com/topic/280379-pagination-page-display/#findComment-1441622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.