Pagination Problem

I have a growing list of information being entered into a database by potential clients, and so Im trying to make it so that only 10 entires are displayed at one time.

I have tried to get it working, and although it recognises the number of entries in the databse and shows this at the bottom of the page as for example page 1 of 3, the full entry list is being displayed rather than the 10.

I have 23 entries at present, and I have set it at 10 per page, and so its showing correctly as page 1 of 3, but for some reason its not restricting the number of entries from the database.

I cant show you the page as its in a secure log in environment, but I do have lal the code below:


<?

if (!(isset($pagenum))) 
 { 
 $pagenum = 1; 
 }
 
$data = mysql_query("SELECT * FROM Register order by RegId desc $max") or die(mysql_error());

$rows = mysql_num_rows($data);

$page_rows = 10;

$last = ceil($rows/$page_rows);

if ($pagenum < 1) 
 { 
 $pagenum = 1; 
 } 
 elseif ($pagenum > $last) 
 { 
 $pagenum = $last; 
 }
 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

while($info = mysql_fetch_array( $data ))
{
   print "First Name :{$info['FirstName']} <br>" .
         "Last Name : {$info['LastName']} <br>" .
		 "Company : {$info['Company']} <br>" .
		 "Address : {$info['Address']} <br>" .
		 "PostCode : {$info['PostCode']} <br>" .
		 "Country : {$info['Country']} <br>" .
		 "Email : <a href=mailto:{$row['Email']}]?subject=TourCheck>{$info['Email']} </a><br>" .
		 "Tel : {$info['Tel']} <br>" . 
         "Fax : {$info['Fax']} <br><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> ";
 } 

 //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> ";
 } 
 ?> 

It seems to be working, but the while print function isnt.

Cheers

Pagination requires 2 queries - 1 to find the total number of entries to be paginated, and 1 to find the current set of records to display. You’ve missed the second.

I’m not one for sprouting unecessary variables, but this is a good trick to adopt;

Change your select line slightly like so:


$qry = "SELECT * FROM Register order by RegId desc $max"
// a line of simple debug so you can see what is going on
// comment it out when not needed or remove completely
echo "<hr>$sql<hr />";
$data = mysql_query($qry) or die(mysql_error());

Then you can investigate what your sql statement contains. We cannot see what $max contains, it should contain text something like;


$max = "LIMIT 10, 10"; 

when you request your 2nd page of 23 items.

When the sql statement is echoed onto the page you can then copy/paste it directly into your database and get further clues as to what is wrong.

Familiarise yourself with the location of your mysql log files too, you should constantly be checking the foot of that file to see what PHP is sending to your database.

Hi StarLion,

Thanks for that…

Any chance of some help. I am teaching myself from a book and whats on the internet.

Cheers

Will give that a go Cups.

Cheers

Hi Cups,

I did what you said, and this is what it came back with.

Query was empty

… means no matching data in your database, never a good way to start …

So what got echoed onto the page?


echo "<hr>$sql<hr />";

nothing unfortunately, only the 2 horizontal lines.

its strange though as the entries do output to the site, so its def reading connecting to the database and outputting the different fields.

Oh dear, my apologies I should have written:


$qry = "SELECT * FROM Register order by RegId desc $max"
// a line of simple debug so you can see what is going on
// comment it out when not needed or remove completely

echo "<hr>$qry<hr />"; <-- GAAHHHH, $qry here

$data = mysql_query($qry) or die(mysql_error()); 

Sorry about that, this stuff is hard enough without getting a bum steer from me … thats 2 bads from me today on here, might go for an early shower…

Hi cups,

Sorry, simple mistake, should have seen it myself.

OK well I tried it, and it came back with:

Resource id #3

Not that makes any sense to me at all.

:confused:

Means the query successfully returned a Resource Object - in this case, a MySQL Result object.

This… is not what you should have gotten if you executed Cups’ code. Show us your code.

OK, here is the whole lot again.

Sorry if I havent done cups suggestion right.

This is a bit of a learning curve for me.

<?

if (!(isset($pagenum))) 
 { 
 $pagenum = 1; 
 }
 
$qry = mysql_query("SELECT * FROM Register order by RegId desc $max") or die(mysql_error());
echo "<hr>$qry<hr />";
$data = mysql_query($qry) or die(mysql_error());

$rows = mysql_num_rows($data);

$page_rows = 10;

$last = ceil($rows/$page_rows);

if ($pagenum < 1) 
 { 
 $pagenum = 1; 
 } 
 elseif ($pagenum > $last) 
 { 
 $pagenum = $last; 
 }
 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

while($info = mysql_fetch_array( $data ))
{
   print "First Name :{$info['FirstName']} <br>" .
         "Last Name : {$info['LastName']} <br>" .
		 "Company : {$info['Company']} <br>" .
		 "Address : {$info['Address']} <br>" .
		 "PostCode : {$info['PostCode']} <br>" .
		 "Country : {$info['Country']} <br>" .
		 "Email : <a href=mailto:{$row['Email']}]?subject=TourCheck>{$info['Email']} </a><br>" .
		 "Tel : {$info['Tel']} <br>" . 
         "Fax : {$info['Fax']} <br><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> ";
 } 

 //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> ";
 } 
 ?>

Come to think of it if you had no choice but to keep one 10 page article in a single table field, I wonder if there could be a way to just write the paging into the article as a link as it is saved?