CrashOkami Posted July 7, 2012 Share Posted July 7, 2012 Hello once more, I'm having trouble with my pagination code. It's all going fine, queries and all, until the time that I have to change page. It just won't change, whether I hit Last or Next. <?php mysql_connect("##", "##","##") or die("Failed to connect."); mysql_select_db("##") or die("Failed to find database."); if (!(isset($pagenum))) { $pagenum = 1; } $data = mysql_query("SELECT * FROM news") or die("Could not execute query."); $rows = mysql_num_rows($data); $page_rows = 30; $last = ceil($rows/$page_rows); if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; $data_p = mysql_query("SELECT * FROM news $max") or die("Could not fetch query data."); while($info = mysql_fetch_array( $data_p )) { Print "<a href='news.php?News_ID=" .$info['News_ID']."'>"; Print $info['Title']; Print "</a>"; Print ", written at "; Print $info['Date']; echo "<br>"; } echo "<p>"; 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> "; } 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> "; } ?> As it is, it fetches 30 results, and I have around 55 in my database. What's wrong when it comes to changing pages though? Can you help me sort this out? Thank you in advance! Quote Link to comment https://forums.phpfreaks.com/topic/265355-help-with-this-code-pagination/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 7, 2012 Share Posted July 7, 2012 Your code isn't setting the $pagenum variable from $_GET['pagenum'] $pagenum = isset($_GET['pagenum']) ? intval($_GET[$pagenum]) : 1; When developing and debugging php code, you need to have php's error_reporting set to E_ALL and display_errors set to ON to get php to help you by reporting and displaying all the errors it detects. You would have been getting an undefined error messages for $pagenum alerting you to the fact that it is not being set in your code. Quote Link to comment https://forums.phpfreaks.com/topic/265355-help-with-this-code-pagination/#findComment-1359897 Share on other sites More sharing options...
CrashOkami Posted July 7, 2012 Author Share Posted July 7, 2012 That's great to know, thank you very much! True, I "had" no errors, I'll go edit the php.ini as you said. Thanks! EDIT: now it refreshes the page, renews the URL, but I'm still getting the results available for page 1 only. Maybe I should not limit the results to 30 the way I do? Quote Link to comment https://forums.phpfreaks.com/topic/265355-help-with-this-code-pagination/#findComment-1359898 Share on other sites More sharing options...
jcbones Posted July 7, 2012 Share Posted July 7, 2012 How many rows is the query returning? Quote Link to comment https://forums.phpfreaks.com/topic/265355-help-with-this-code-pagination/#findComment-1359916 Share on other sites More sharing options...
CrashOkami Posted July 7, 2012 Author Share Posted July 7, 2012 It prints 30 records ($rows = mysql_num_rows($data); $page_rows = 30; ), but now I've set it to print as many as the database has ($page_rows = $rows;) until I fix this issue. I really can't seem to find it. Quote Link to comment https://forums.phpfreaks.com/topic/265355-help-with-this-code-pagination/#findComment-1359920 Share on other sites More sharing options...
PFMaBiSmAd Posted July 7, 2012 Share Posted July 7, 2012 Your code works for me, with some test data with ~50 rows. The only thing I noticed was a typo in the line of code I posted, which you would have also noticed if your error_reporting/display_errors settings were already set as suggested. While the error in the line of code I posted was not intentional, this points out why you need to have php's error_reporting and display_errors set as suggested to save you a huge amount of time when debugging. If you changed your master php.ini settings, you must stop and start your web server to get any changes made to take effect and you need to confirm using a phpinfo statement that the settings actually got changed in case the php.ini that php is using is not the one that you changed. Quote Link to comment https://forums.phpfreaks.com/topic/265355-help-with-this-code-pagination/#findComment-1359946 Share on other sites More sharing options...
CrashOkami Posted July 8, 2012 Author Share Posted July 8, 2012 I got "undefined variable" and "index", and I wrote the code like this: <?php $pagenum = isset($_GET['pagenum']) ? intval($_GET['pagenum']) : 1; ?> Seems to be working now, can you please clarify that this is what you also did? Just to see if I got it right. Thanks a ton for your help Quote Link to comment https://forums.phpfreaks.com/topic/265355-help-with-this-code-pagination/#findComment-1360033 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.