Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/265355-help-with-this-code-pagination/
Share on other sites

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.

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?

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.

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.