PHP MySql Pagination Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

PHP MySql Pagination Problem

Post by nita »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi.
I have a problem with pagination of my mysql database results.
Script is printing the first 10 results and links to the rest with no problem.
But once i press on the link is putting me back to root of application, instead of paging  out  the rest of results.

to test it go to :
[url]http://www.nita-on-line.com/movies/[/url]

Code: Select all

if(isset($_GET['cat'])) // when looking at selected category of movies
{

	// printing out menu with categories
	$result = mysql_query("SELECT * FROM category ORDER BY cat") or die(mysql_error());
	while($row = mysql_fetch_array( $result )) 
	{
	echo "<A href='index.php?cat=$row[cat]'>$row[cat]</a><br>";
	}

	$rowsPerPage = 10;
	$pageNum = 1;
	
	if(isset($_GET['page']))
	{
    $pageNum = $_GET['page'];
	}
	$offset = ($pageNum - 1) * $rowsPerPage;
	
	$cat=$_GET['cat'];
 	$result=mysql_query("SELECT * FROM movies WHERE cat='$cat' ORDER BY name LIMIT $offset, $rowsPerPage") or die(mysql_error());
 	while($row=mysql_fetch_array($result))
 	{
	include "display_rec.php";
	}

$query   = "SELECT COUNT(name) AS numrows FROM movies WHERE cat='$cat'";
$result  = mysql_query($query) or die('Error, query failed');
$row1     = mysql_fetch_array($result, MYSQL_ASSOC);

$numrows = $row1['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];

if ($pageNum > 1)
{
    $page = $pageNum - 1;
    $prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
    
    $first = " <a href=\"$self?page=1\">[First Page]</a> ";
} 
else
{
    $prev  = '';
    $first = '';
}

if ($pageNum < $maxPage)
{
    $page = $pageNum + 1;
    $next = " <a href=\"$self.php?page=$page\">[Next]</a> ";
    
    $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} 
else
{
    $next = '';
    $last = '';
}   
echo $first . $prev . " Page $pageNum of $maxPage " . $next . $last;
}
if someone can help me to find a bug in this script, please

Thanks a lot in advance.

Nita


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

The reason the version you linked to isn't working is because the links to First, Prev, and Last are missing the 'cat' variable in the link. The code you posted here doesn't seem to have the same issue though. I can only assume that the version online and the code you posted are different.
nita
Forum Commoner
Posts: 39
Joined: Wed May 02, 2007 5:49 am

Its all good now

Post by nita »

thx for replay

i just got it fixed

and you right all i had to do is add cat variable to the links,

thx again

nita
Post Reply