Trying to get to the bottom of a paging 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
slaterino
Forum Commoner
Posts: 46
Joined: Fri Jul 11, 2008 10:50 am

Trying to get to the bottom of a paging problem

Post by slaterino »

Hi,
I've been working on a query that allows for a number of GET queries from links and a search function. I have almost got the query working perfectly except for the $maxPage field, which forms part of the paging function. For some reason this is defaulting to 1 and I can't work out why. I have included the code below. The last line echoes the $maxPage field. For some reason this always shows as 1, so for example when the page is catalogue.php?page=1 the last line echoes 'Showing Page 1 of 1'. Then also if the page is catalogue.php?page=22 the line would echo 'Showing Page 22 of 1 Pages.' Can anyone spot would the error might be?

Code: Select all

$var = @$_GET['qsearch'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
 
$rowsPerPage = 7;
$pageNum = 1;
 
if(isset($_GET['page']))
{
   $pageNum = $_GET['page'];
}
 
$offset = ($pageNum - 1) * $rowsPerPage;
 
$self = $_SERVER['PHP_SELF'];
 
$catalogue_query  = "Product_ID, Genus, Division FROM products";
if (isset($_GET['qsearch'])) {
$catalogue_query .=" WHERE Common_Name OR Genus like \"%$trimmed%\"";
}
else {
if (isset($_GET['Genus'])) {
$catalogue_query .=" WHERE Genus = '" .mysql_real_escape_string($_GET['Genus']). "'";
}
if (isset($_GET['Division'])) {
$catalogue_query .=" AND Division = '" .mysql_real_escape_string($_GET['Division']). "'";
}
}
$result = mysql_query("SELECT COUNT(Product_ID) AS numrows, $catalogue_query GROUP BY Product_ID") or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
 
$catalogue_query .= " ORDER BY Product_ID DESC LIMIT $offset, $rowsPerPage";
$result = mysql_query("SELECT ".$catalogue_query) or die('Error, query failed');
 
echo " Showing page $pageNum of $maxPage pages ";
 
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
Thanks!
Russ
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: Trying to get to the bottom of a paging problem

Post by it2051229 »

Hmm... i'm not sure about this

Code: Select all

 
$result = mysql_query("SELECT COUNT(Product_ID) AS numrows, $catalogue_query GROUP BY Product_ID") or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
 
mind giving a test for me like change it to something like

Code: Select all

 
$result = mysql_query("SELECT COUNT(Product_ID), $catalogue_query GROUP BY Product_ID") or die('Error, query failed');
$numrows     = mysql_result($result, 0, "COUNT(Product_ID)");
$maxPage = ceil($numrows/$rowsPerPage);
 
Post Reply