I have a site that has alot of pages, but it navigation is becoming too big. At the more its like " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32". Now I want it to be like << 1 2 [3] 4 5 >> and lets say you was on page 20 it would be << 16 17 [20] 21 22 >>. I have a basic pagin system at the moment how would i add this? The site is www.ps3.sc
Something like the following should work $current_page = $_GET['p']; //Page display width - number of pages on each side of the current $page_width = 2; //Echo code for previous page echo "<a href='http://www.ps3.sc/?p=" . ($current_page - 1) . "'> < </a>"; $x = $current_page - $page_width; while ($x <= ($current_page + $page_width)) { if ($x == $current_page) { echo " [" . $x "] "; } else { echo " <a href='http://www.ps3.sc/?p=" . $x . "'>" . $x . "</a> "; } $x++; } //Echo code for next page echo "<a href='http://www.ps3.sc/?p=" . ($current_page + 1) . "'> > </a>"; PHP:
Also note that this question has been asked (and answered) PLENTY of times... just do a search and you'll get a plethora of answers!
Streety, Thanks I've got it working. But its like this << Prev -3 -2 [ -1 ] 0 1 Next >>, see the minus numbers? I have tried several ways to remove them when the page is one or below but no luck, any help?
No problem, try this . . . <?php if(isset($_GET['p'])) { $current_page = $_GET['p']; } else { $current_page = 1; } //Page display width - number of pages on each side of the current $page_width = 2; //Maximum and minimum page numbers $min_page = 1; $max_page = 10; //Echo code for previous page if (($current_page - 1) >= $min_page) { echo "<a href='http://www.ps3.sc/?p=" . ($current_page - 1) . "'> < </a>"; } $x = $current_page - $page_width; while ($x <= ($current_page + $page_width)) { if ($x == $current_page) { echo " [" . $x . "] "; } elseif (($x < $min_page) || ($x > $max_page)) { //Do nothing } else { echo " <a href='http://www.ps3.sc/?p=" . $x . "'>" . $x . "</a> "; } $x++; } //Echo code for next page if (($current_page + 1) <= $max_page) { echo "<a href='http://www.ps3.sc/?p=" . ($current_page + 1) . "'> > </a>"; } ?> PHP: You would get the same problem at high numbers (going to pages you haven't created yet) but this script should fix that side of things as well. I don't know how you store your pages so I've just put a value (10) in for the moment. You could either manually update it each time you add a page or set it dynamically by counting the rows in your database table or files in your directory depending on what you use.