heldenbrau Posted August 6, 2009 Share Posted August 6, 2009 On a forum like this, at the bottom it shows page numbers. I am working on my own forum at the moment and I can't figure out how these pages can be generated. My brain can't handle it. Is there an easy way? Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/ Share on other sites More sharing options...
jonsjava Posted August 6, 2009 Share Posted August 6, 2009 It's called Pagnation. it generates the total number of pages based upon the number of items in the database/ the number to show per page. Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/#findComment-892501 Share on other sites More sharing options...
kickstart Posted August 6, 2009 Share Posted August 6, 2009 Hi When I have needed to do it I have set up an array. Put the numbers 1 to 5 into it, and the last 5, and then the page numbers either side of the current page. And then used array_unique to discard the duplicates. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/#findComment-892503 Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 there is a tutorial on this site. look up pagination. Also, if you can't do pagination, you may have trouble making an entire forum from scratch. good luck! Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/#findComment-892504 Share on other sites More sharing options...
heldenbrau Posted August 6, 2009 Author Share Posted August 6, 2009 ok, thanks, will read up on pagination. Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/#findComment-892508 Share on other sites More sharing options...
heldenbrau Posted August 7, 2009 Author Share Posted August 7, 2009 I found some info on the internet to help me and I copied their code into one of my own pages. I am sure that I have copied it properly. But what happens is, it only ever shows the first lot of results, no matter what page it is on. If I click next page, the pagenum goes to 2, but the "<--prev" link doesn't appear and it shows the same database results as it did for page 1. This is the code: <html> <head> <style type="text/css">@import url(styles/cases.css);</style> </head> <body> <div id="masthead"> <!--#include file="cases/header.html"--> </div> <div id="sidebar"> <!--#include file="cases/sidebar.html"--><br> </div> <?php date_default_timezone_set('Europe/London'); if (!(isset($pagenum))) { $pagenum = 1; } $time = mktime()-7776000; $mysqli = new mysqli("localhost", "user", "password", "cases"); if ($mysqli === false) { die("ERROR: Could not connect to database. " . mysqli_connect_error()); } $sql="SELECT * FROM cases WHERE lastpost>$time"; if ($result = $mysqli->query($sql)) { if ($result->num_rows > 0) { $rows = mysqli_num_rows($result); }else {echo "No records matching your query were found."; } }else { echo "ERROR: Could not execute $sql . " . $mysqli->error; } $page_rows = 15; $last = ceil($rows/$page_rows); if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } //This sets the range to display in our query $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; $sql="SELECT casenum, heading,lastpost FROM cases WHERE lastpost>$time ORDER BY lastpost DESC $max"; if ($result = $mysqli->query($sql)) { if ($result->num_rows > 0) { while($row = $result->fetch_array()){ $date= date("d M Y H:i:s", $row[2]); echo"<div id=\"content\">\n <a href=\"cases/case$row[0].shtml\"<font size=\"4\">$row[1]</a> | Udated $date BST</font>\n </div>"; } $result->close(); }else { echo "No records matching your query were found."; } }else { echo "ERROR: Could not execute $sql . " . $mysqli->error; } $mysqli->close(); echo " --Page $pagenum of $last-- <p>"; if ($pagenum == 1) { } else { echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; echo " "; $previous = $pagenum-1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; } //just a spacer echo " ---- "; if ($pagenum == $last) { } else { $next = $pagenum+1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; echo " "; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; } ?> <div id="footer"> <!--#include file="cases/footer.html" --> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/#findComment-892829 Share on other sites More sharing options...
kickstart Posted August 7, 2009 Share Posted August 7, 2009 Hi Unless you have set register_globals on (generally recommended against) then it appears that you do not set $pagenum anywhere. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/#findComment-892837 Share on other sites More sharing options...
heldenbrau Posted August 7, 2009 Author Share Posted August 7, 2009 I set pagenum in this bit if (!(isset($pagenum))) { $pagenum = 1; } Does it need to be set before the if? Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/#findComment-892917 Share on other sites More sharing options...
kickstart Posted August 7, 2009 Share Posted August 7, 2009 Hi That is only ever going to set it to 1, as it won't be set before the if statement (unless register_globals is on, which is generally strongly recommended against, even though it used to be the standard). What you need is something like:- $pagenum = intval($_REQUEST['pagenum']); if ($pagenum < 1 ) { $pagenum = 1; } All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/#findComment-893031 Share on other sites More sharing options...
heldenbrau Posted August 7, 2009 Author Share Posted August 7, 2009 Hi, thanks for that, now everything is working perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/169151-solved-how-is-it-done/#findComment-893050 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.