Forum Moderators: coopster

Message Too Old, No Replies

Problem with pagination script in PHP

PHP, pagination, search results

         

galahad2

9:33 am on Sep 22, 2009 (gmt 0)

10+ Year Member



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

?>

whoisgregg

10:30 pm on Sep 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



eelixduppy posted a great thread about PHP/MySQL pagination [webmasterworld.com] a few years back. It's thoroughly commented and helps to clarify the exact process used to generate the right output. (It also links to a bunch of other threads on this same subject.)