Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP Pagination

Posted on 2009-02-19
11
Medium Priority
?
405 Views
Last Modified: 2012-05-06
I have been trying for weeks to get this pagination script to work. I have been able to LIMIT the amount of entries to 20 but my next>> button will not navigate to page 2!

can anyone help with this? I think it has something to do with the echo statement...
<?php
 
mysql_connect("", "", "") or die(mysql_error()); 
mysql_select_db("content") or die(mysql_error());
 
(mysql_error()); 
 
//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 
 
//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT id, title, description FROM musthave") or die(mysql_error()); 
$rows = mysql_num_rows($data); 
 
//This is the number of results displayed per page 
$page_rows = 20; 
 
//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; 
 
// if no id is specified, list the available articles
if(!isset($_GET['id']))
{
   $self = $_SERVER['PHP_SELF'];
   $start = $pagenum - 1;
   $query = "SELECT id, title, description FROM musthave ORDER BY title LIMIT $start,20 ";
 
   $results = mysql_query($query) or die('Error : ' . mysql_error()); 
 
   // create the article list 
   $description = '<ol>';
   while($row = mysql_fetch_array($results, MYSQL_NUM))
   {
      list($id, $title) = $row;
      $description .= "<li><a href=\"$self?id=$id\">$title</a></li>\r\n";
   }
 
   $description .= '</ol>';
 
   $title = 'Ladies watches';
} else {
   // get the article info from database
   $query = "SELECT title, description FROM musthave WHERE id=".$_GET['id'];
   $results = mysql_query($query) or die('Error : ' . mysql_error()); 
   $row = mysql_fetch_array($results, MYSQL_ASSOC); 
 
   $title = $row['title'];
   $desription = $row['desription'];
} 
 
 
?>
 
<?php
 
 
$cacheDir = dirname(__FILE__) . '/cache/';
 
if (isset($_GET['id'])) {
   $cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';
} else {
   $cacheFile = $cacheDir . 'index.html';
} 
 
if (file_exists($cacheFile))
{
   header("Content-Type: text/html");
   readfile($cacheFile);
   exit;
}
 
// ... more code coming
 
if(!isset($_GET['id']))
{
   $self   = $_SERVER['PHP_SELF'];
   $start = $pagenum - 1;
   $query = "SELECT id, title, description FROM musthave ORDER BY title LIMIT $start,20 ";
   $results = mysql_query($query) or die('Error : ' . mysql_error()); 
 
   $description = '<ol>';
   while($row = mysql_fetch_array($results, MYSQL_NUM))
   {
      list($id, $title) = $row;
      $description .= "<li><a href=\"$self?id=$id\">$title</a></li>\r\n";
   }
 
   $description .= '</ol>';
 
   $title = 'Ladies Watches';
} else {
   // get the article info from database
   $query  = "SELECT title, description FROM musthave WHERE id=".$_GET['id'];
   $results = mysql_query($query) or die('Error : ' . mysql_error()); 
   $row    = mysql_fetch_array($results, MYSQL_ASSOC); 
 
   $title = $row['title'];
   $description = $row['description'];
} 
 
// ... still more code coming
 
ob_start();
 
 
?>
herre is the echo script with pagination:
<div class="articles">
<h3 align="center">
<?php echo $title; ?></h3>
<?php 
echo $description;
// when displaying an article show a link
// to see the article list
if(isset($_GET['id']))
{ 
?>
<p>&nbsp;</p>
<p align="center">
  <a href="<?php echo $_SERVER['PHP_SELF']; ?>">Back to top</a></p>
<?php
}
?>
 
<?php
 
// 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='{$_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 "";
 
//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='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
} 
?>

Open in new window

0
Comment
Question by:rafique12
  • 5
  • 4
  • 2
11 Comments
 
LVL 1

Expert Comment

by:php-ninja
ID: 23682964
Hi,
Have you got this page up on a server anywhere?
Could you link me to it so I can see more precisely where it's going wrong?
Thanks,
0
 
LVL 1

Expert Comment

by:php-ninja
ID: 23683023
Actually, I think I might see the problem already, try changing the start of your code as follows:
<?php
 
mysql_connect("", "", "") or die(mysql_error()); 
mysql_select_db("content") or die(mysql_error());
 
(mysql_error()); 
 
if(isset($_GET['pagenum'])){
$pagenum = $_GET['pagenum'];
} 
 
//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

Open in new window

0
 

Author Comment

by:rafique12
ID: 23683998
Some things changed but it is not actually moving on to the next page. I still displays the first 20 entries! despite the page navigation moving to the next page. SORRY! the site is still on the locahost!
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 

Author Comment

by:rafique12
ID: 23684074
I think the echo statement is overiding any LIMIT that gets set and is simply echoing the first 20 entries on every page!
0
 
LVL 1

Expert Comment

by:php-ninja
ID: 23684404
Look at the $start variable, its taking the page number -1, so if we are on page 2 it will still start from row 1!

Instead of:
$start = $pagenum - 1;

Try replacing it with:
$start = $pagenum -1 * $page_rows;

That will mean that if we're on page 2 it will start on row 20, which is what we want.
Let me know if this helps.
0
 

Author Comment

by:rafique12
ID: 23684599
The code is only showing the first set of entires even if we scroll to the next>> page. I will send the code again with the suggested changes.
<?php
 
mysql_connect("127.0.0.1", "root", "rafiqd") or die(mysql_error()); 
mysql_select_db("content") or die(mysql_error());
 
(mysql_error()); 
 
if(isset($_GET['pagenum'])){
$pagenum = $_GET['pagenum'];
} 
 
//This checks to see if there is a page number. If not, it will set it to page 1 
if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 
 
//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT id, title, description FROM musthave") or die(mysql_error()); 
$rows = mysql_num_rows($data); 
 
//This is the number of results displayed per page 
$page_rows = 3; 
 
//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; 
 
// if no id is specified, list the available articles
if(!isset($_GET['id']))
{
   $self = $_SERVER['PHP_SELF'];
   $start = $pagenum -1 * $page_rows;
   $query = "SELECT * FROM musthave ORDER BY title LIMIT 0,3 ";
   $results = mysql_query($query) or die('Error : ' . mysql_error()); 
 
   // create the article list 
   $description = '<ol>';
   while($row = mysql_fetch_array($results, MYSQL_NUM))
   {
      list($id, $title) = $row;
      $description .= "<li><a href=\"$self?id=$id\">$title</a></li>\r\n";
   }
 
   $description .= '</ol>';
 
   $title = 'Ladies watches';
} else {
   // get the article info from database
   $query = "SELECT title, description FROM musthave WHERE id=".$_GET['id'];
   $results = mysql_query($query) or die('Error : ' . mysql_error()); 
   $row = mysql_fetch_array($results, MYSQL_ASSOC); 
 
   $title = $row['title'];
   $desription = $row['desription'];
} 
 
 
?>
 
<?php
 
 
$cacheDir = dirname(__FILE__) . '/cache/';
 
if (isset($_GET['id'])) {
   $cacheFile = $cacheDir . '_' . $_GET['id'] . '.html';
} else {
   $cacheFile = $cacheDir . 'index.html';
} 
 
if (file_exists($cacheFile))
{
   header("Content-Type: text/html");
   readfile($cacheFile);
   exit;
}
 
// ... more code coming
 
if(!isset($_GET['id']))
{
   $self   = $_SERVER['PHP_SELF'];
   $start = $pagenum -1 * $page_rows;
   $query  = "SELECT * FROM musthave ORDER BY title LIMIT 0,3";
   $results = mysql_query($query) or die('Error : ' . mysql_error()); 
 
   $description = '<ol>';
   while($row = mysql_fetch_array($results, MYSQL_NUM))
   {
      list($id, $title) = $row;
      $description .= "<li><a href=\"$self?id=$id\">$title</a></li>\r\n";
   }
 
   $description .= '</ol>';
 
   $title = 'Ladies Watches';
} else {
   // get the article info from database
   $query  = "SELECT title, description FROM musthave WHERE id=".$_GET['id'];
   $results = mysql_query($query) or die('Error : ' . mysql_error()); 
   $row    = mysql_fetch_array($results, MYSQL_ASSOC); 
 
   $title = $row['title'];
   $description = $row['description'];
} 
 
// ... still more code coming
 
ob_start();
 
 
?>
This is where the contents is shown onpage using an echo atatement:
<h3 align="center">
<?php echo $title; ?></h3>
<?php 
echo $description;
// when displaying an article show a link
// to see the article list
if(isset($_GET['id']))
{ 
?>
<p>&nbsp;</p>
<p align="center">
  <a href="<?php echo $_SERVER['PHP_SELF']; ?>">Back to top</a></p>
<?php
}
?>
And here is the page links:
<?php
 
// 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='{$_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 "";
 
//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='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
} 
?>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 23685962
the limit addition limits the result for query like: FROM ROW, HOW MANY ROWS

so you cannot
   $self = $_SERVER['PHP_SELF'];
   $start = $pagenum -1 * $page_rows;
   $query = "SELECT * FROM musthave ORDER BY title LIMIT 0,3 ";

rather

   $self = $_SERVER['PHP_SELF'];
   $start = $pagenum -1 * $page_rows;
   $query = "SELECT * FROM musthave ORDER BY title LIMIT $start,3 ";
0
 
LVL 1

Expert Comment

by:php-ninja
ID: 23686021
Yes, you shouldn't have changed it from $start to 0 lol.
Please change it back to :
$query = "SELECT * FROM musthave ORDER BY title LIMIT $start,3 ";

or better still:

$query = "SELECT * FROM musthave ORDER BY title LIMIT $start, $page_rows";
0
 

Author Comment

by:rafique12
ID: 23689232
I changed it back now I am getting an error: Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-2,3' at line 1
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 2000 total points
ID: 23689538
start should be
 $start = $pagenum -1;
not
 $start = $pagenum -1 * $page_rows;

also you are getting -2,3 because
 $start = $pagenum  (-1 * $page_rows);

0
 

Author Closing Comment

by:rafique12
ID: 31548707
That fixed it Pal :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question