mospeed Posted April 6, 2010 Share Posted April 6, 2010 The pagination script that I have has been working great but now that I have over 20 pages it looks like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Next whereas I'd like to limit it to look something like this: 1 2 3 4 5 6 7 8 9 ... 19 20 Next What do I need to add to it to do this? For a refence, this is my pagination file here: $display = 9; $pages = ceil($num/$display); $current_page = $id; if ($current_page != 1) { echo '<a href="index.php?p=' . ($id - 1) . '">Previous</a> '; } for ($i = 1; $i <= $pages; $i++) { if ($i != $current_page) { echo '<a href="index.php?p=' . (($pages * ($i / $pages))) . '">' . $i . '</a> '; } else { echo $i . ' '; } } // End for FOR loop. // If it's not the last page, make a next button: if ($current_page != $pages) { echo '<a href="index.php?p=' . ($id + 1) . '"> Next</a>'; } Quote Link to comment Share on other sites More sharing options...
Jax2 Posted April 6, 2010 Share Posted April 6, 2010 My pagination shows however many pages on either side of the current page that you set as $range. For example, my range is set to 5, if I am on page 10, my link would look like this: << < [5][6][7][8][9] 10 [11][12][13][14][15] > >> Here's the code, though you may have to edit it a bit to suit your needs, or you can take from it and use on your own as you like $rowsperpage = 10; // how many items per page $range = 10;// how many pages to show in page link if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; $sql = "SELECT COUNT(*) FROM "BLA BLA BLA"; $result = mysql_query($sql, $db) or die(mysql_error()); $r = mysql_fetch_row($result); $numrows = $r[0]; $totalpages = ceil($numrows / $rowsperpage); if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if PUT YOUR SQL CODE HERE AS WELL AS EVERYTHING THAT SHOULD BE ON THE PAGE. THE FOLLOWING GOES BELOW AT THE BOTTOM OF YOUR CONTENT: $result = mysql_query("SELECT * FROM BLA BLA BLA, $db); $num_rows = mysql_num_rows($result); if ($num_rows<1) { ?> <?php } ELSE { echo "Page ".$currentpage." of ".$totalpages."<br>"; if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='YOURPAGE.php?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='YOURPAGE.php?currentpage=$prevpage'><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='YOURPAGE.php?currentpage=$x'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " <a href='YOURPAGE.php?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='YOURPAGE.php?currentpage=$totalpages'>>></a> "; } // end if } // end else } So, in this, simply change the BLA BLA BLA to your SQL queries, and YOURPAGE.php to whatever page you're using the code on. Quote Link to comment Share on other sites More sharing options...
mospeed Posted April 6, 2010 Author Share Posted April 6, 2010 Wow thanks a lot, this worked like a charm and was very easy for me to understand. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.