...

View Full Version : Problem with pagination script in PHP



galahad3
09-22-2009, 10:38 AM
I'm using a PHP script to paginate results from a db query over a number of pages. The only thing is that the "Next" and "Last" links for additional results don't seem to work at all (they just return the same page, i.e page 1).

Also I need to figure how I can modify this script so that it shows pages 1 to 4 as well as a First, Last, Prev and Next.

This is the current script:




<?php
include ('manager/special_offers/inc/dbconnect.php');


//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

// Build SQL Query
$data = mysql_query("select * from specialofferstable WHERE category like 'hotel' ORDER BY price") or die(mysql_error()); // specify the table and field names for the SQL query
//$numresults=mysql_query($query);
// get results
//$result = mysql_query($query) or die("Couldn't execute query");

$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 15;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
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;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("select * from specialofferstable WHERE category like 'hotel' ORDER BY price $max") or die(mysql_error());

// display the results returned
while ($row= mysql_fetch_array($data_p)) {
$title = $row["category"];
$title2 = $row["company_hotel"];
$title3 = $row["location"];
$title4 = $row["offer"];
$title5 = $row["price"];
$title6 = $row["offerends"];
$title7 = $row["mobile"];
$dateformat = date("M j Y" ,strtotime($title6));


echo '<h3>'.$title2.' | '.$title3.' <em class=grey>(Offer ends: '.$dateformat.')</em></h3><h4>'.$title5.' <strong class=call>Call 0844 793 7300</strong></h4>
<p>'.$title4.'</p><hr />' ;

$count++ ;
}
// This shows the user what page they are on, and the total number of pages
echo "Page $pagenum of $last<p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='specialoffers.html?pagenum=1'> << First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='specialoffersall.html?pagenum=$previous'> << Previous</a> ";
}

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='specialoffersall.html?pagenum=$next'>Next >></a> ";
echo " ";
echo " <a href='specialoffersall.html?pagenum=$last'>Last >></a> ";
}

?>

abduraooft
09-22-2009, 11:08 AM
Please edit your post and wrap PHP codes using the
][/COLOR] tag (http://www.codingforums.com/showthread.php?t=68462)

galahad3
09-22-2009, 11:18 AM
Done, hopefully that makes it easier to read through.

abduraooft
09-22-2009, 11:29 AM
Have you testing on a server where register_globals turned on (I wouldn't recommend doing that, otherwise)? If not, change
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; to


if(isset($_GET['pagenum']))
$pagenum =(int) $_GET['pagenum'];
else
$pagenum =0;

//This sets the range to display in our query
$max = 'limit ' .($pagenum ) * $page_rows .',' .$page_rows;

PS: When considering efficiency, using SELECT * for fetching rows is a bad practice, and using the same for getting the total number if rows (for mysql_num_rows) is even worse. Instead of that use

SELECT count(*) as row_count .... and then get the value of row_count

galahad3
09-22-2009, 12:16 PM
Thanks, have tried adding that code, I'm still getting the same page when I click theb "Next" link (and the "Last" link in fact).

"Next" links to ?pagenum=1 (when on the first page of results), and "Last" links to ?pagenum=2 (which is correct but when I click on it, it just shows the first page).

I also noticed that the "First" link also links to ?pagenum=1 (which is fine but then I wonder why "Next" is also linking to the first page?)

Also the accompanying text reads "Page 0 of 2" which is a bit odd.

fifth
09-22-2009, 11:00 PM
[Edit] I should really read all the posts in a thread :p , so yeah, what abduraooft said.

But from what your describing it sounds like the problem is stemming from the $last variable. Try;



// Build SQL Query
$data = mysql_query("select count(*) from specialofferstable WHERE category like 'hotel'") or die(mysql_error()); // specify the table and field names for the SQL query
//$numresults=mysql_query($query);
// get results
//$result = mysql_query($query) or die("Couldn't execute query");

$count_result = mysql_fetch_row($data);
$rows = $count_result[0];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum