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))Russ