Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-22-2009, 10:38 AM   PM User | #1
galahad3
Regular Coder

 
Join Date: Sep 2009
Posts: 160
Thanks: 15
Thanked 0 Times in 0 Posts
galahad3 is an unknown quantity at this point
Problem with pagination script in PHP

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

<?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 ($rowmysql_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> ";


?>

Last edited by galahad3; 09-22-2009 at 11:18 AM..
galahad3 is offline   Reply With Quote
Old 09-22-2009, 11:08 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Please edit your post and wrap PHP codes using the [PHP][/PHP] tag
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 09-22-2009, 11:18 AM   PM User | #3
galahad3
Regular Coder

 
Join Date: Sep 2009
Posts: 160
Thanks: 15
Thanked 0 Times in 0 Posts
galahad3 is an unknown quantity at this point
Done, hopefully that makes it easier to read through.
galahad3 is offline   Reply With Quote
Old 09-22-2009, 11:29 AM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Have you testing on a server where register_globals turned on (I wouldn't recommend doing that, otherwise)? If not, change
PHP Code:
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

PHP Code:
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
Code:
SELECT count(*) as row_count ....
and then get the value of row_count
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 09-22-2009, 12:16 PM   PM User | #5
galahad3
Regular Coder

 
Join Date: Sep 2009
Posts: 160
Thanks: 15
Thanked 0 Times in 0 Posts
galahad3 is an unknown quantity at this point
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.

Last edited by galahad3; 09-22-2009 at 12:19 PM..
galahad3 is offline   Reply With Quote
Old 09-22-2009, 11:00 PM   PM User | #6
fifth
New Coder

 
Join Date: Jun 2009
Posts: 20
Thanks: 0
Thanked 2 Times in 2 Posts
fifth is an unknown quantity at this point
[Edit] I should really read all the posts in a thread , so yeah, what abduraooft said.

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

PHP Code:
// 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]; 

Last edited by fifth; 09-22-2009 at 11:24 PM..
fifth is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:47 PM.


Advertisement
Log in to turn off these ads.