Jump to content

Pagination doesnt work...


phpSensei

Recommended Posts

Everything is fine except for one thing....THE PAGE DOESNT CHANGE!

 

The url and stuff is fine, but when you click next page the data stays the same..

 

<?php
$localhost= "localhost";
$user= "root";
$password="";
$database= "dbase";
mysql_connect($localhost,$user,$password) or die(mysql_error());
mysql_select_db($database);
//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 * FROM linklist") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

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

//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; 

$localhost= "localhost";
$user= "root";
$password="";
$database= "dbase";
mysql_connect($localhost,$user,$password) or die(mysql_error());
mysql_select_db($database);


$data_p = mysql_query("SELECT * FROM linklist $max") or die(mysql_error()); 

//This is where you display your query results
while($info = mysql_fetch_array( $data_p )) 
{ 
Print $info['list_title']; 
echo "<br>";
} 
echo "<p>";

// 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='shortext.php?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='shortext.php?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> ";
} 
?> 

Link to comment
https://forums.phpfreaks.com/topic/62588-pagination-doesnt-work/
Share on other sites

Here's the problem:

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

 

That code will only work if you have GLOBALS turned on - which it shouldn't be.

 

Try this:

 

//This checks to see if there is a page number. If not, it will set it to page 1 
$pagenum = (!isset($_GET['pagenum']))?1:$pagenum;

 

Of course, you should probably validate that $_GET['pagenum'] is a whole number greater than 0 as well.

 

This should work:

 

<?php
$localhost= "localhost";
$user= "root";
$password="";
$database= "dbase";
mysql_connect($localhost,$user,$password) or die(mysql_error());
mysql_select_db($database);


$rows_per_page = 10;

if (!isset($pagenum) || $pagenum < 1) { 
$pagenum = 1; 
} 

$data = mysql_query("SELECT count(*) FROM linklist") or die(mysql_error()); 
$rows = mysql_result($data, 0);

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

if ($pagenum > $total_pages) {
$pagenum = $total_pages;
}

$max = 'limit ' . ($pagenum - 1) * $rows_per_page . ',' . $rows_per_page; 

$data_p = mysql_query("SELECT * FROM linklist $max") or die(mysql_error()); 

while ($info = mysql_fetch_array( $data_p )) { 
echo $info['list_title'] . "<br />"; 
} 

echo "<p>--Page $pagenum of $total_pages-- <p>";

if ($pagenum != 1) {
echo " <a href='shortext.php?pagenum=1'> <<-First</a> <a href='shortext.php?pagenum=" . $pagenum - 1 . "'> <-Previous</a> ";
} 

echo " ---- ";

if ($pagenum != total_pages) {
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=" . $pagenum + 1 . "'>Next -></a> <a href='{$_SERVER['PHP_SELF']}?pagenum=$total_pages'>Last ->></a> ";
} 
?> 

 

You should never do "SELECT * FROM table" just to get the number of rows.  It creates substantial extra work for the database, and if there is a lot of results, they all must be returned to the web server which generates extra network traffic unnecessarily.  Bottom line, do like above, use a count query, then get the result.

Warning: Division by zero in C:\wamp\www\movies_website\files\shortext.php on line 19
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 '-10,10' at line 1

 

Line 19:

16 : $data = mysql_query("SELECT count(*) FROM linklist") or die(mysql_error()); 
17 : $rows = mysql_result($data, 0);
18 : 
19 : $total_pages = ceil($rows/$page_rows); 

I didn't spell the variable name correctly....there was also another syntax error...

 

<?php
$localhost= "localhost";
$user= "root";
$password="";
$database= "dbase";
mysql_connect($localhost,$user,$password) or die(mysql_error());
mysql_select_db($database);


$rows_per_page = 10;

if (!isset($pagenum) || $pagenum < 1) { 
$pagenum = 1; 
} 

$data = mysql_query("SELECT count(*) FROM linklist") or die(mysql_error()); 
$rows = mysql_result($data, 0);

$total_pages = ceil($rows / $rows_per_page); 

if ($pagenum > $total_pages) {
$pagenum = $total_pages;
}

$max = 'limit ' . ($pagenum - 1) * $rows_per_page . ',' . $rows_per_page; 

$data_p = mysql_query("SELECT * FROM linklist $max") or die(mysql_error()); 

while ($info = mysql_fetch_array( $data_p )) { 
echo $info['list_title'] . "<br />"; 
} 

echo "<p>--Page $pagenum of $total_pages-- <p>";

if ($pagenum != 1) {
echo " <a href='shortext.php?pagenum=1'> <<-First</a> <a href='shortext.php?pagenum=" . $pagenum - 1 . "'> <-Previous</a> ";
} 

echo " ---- ";

if ($pagenum != $total_pages) {
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=" . $pagenum + 1 . "'>Next -></a> <a href='{$_SERVER['PHP_SELF']}?pagenum=$total_pages'>Last ->></a> ";
} 
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.