PHP MySql Pagination Problem
Posted: Sat May 05, 2007 4:36 am
feyd | Please use
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]
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;
}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]