I am having problems with my search results page!
I am pulling records from a MySQL database. This page has the search form on it: www.lighthousepublishing.org/Store/index-2.php. This page posts data to [url]http://www.lighthousepublishing.org...inventory-2.php.[/url]
Everything worked fine when I dumped all the results on one page, but it takes so long to load, so I want to allow the user to page through the results with 10 records on a page. Since I added the paging links to searchinventory-2.php, the first page's results are perfect. But whenever I click on "Next" to go to page 2, it never shows the correct records! It seems like it always shows the same records from page 2 and any pages after that (no matter which records you tell it to display).
If there is any PHP programmer that could tell me what is wrong with my code I would appreciate it so very, very much!!! I have been struggling with this page too long. Please keep the answer simple and detailed so I can understand as I am a newbie in PHP.
Here is a section of the code on searchinventory-2.php (the part that allows paging).
PHP:
$queryString_SearchInventory = "";
if (!empty($HTTP_SERVER_VARS['QUERY_STRING'])) {
$params = explode("&", $HTTP_SERVER_VARS['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_SearchInventory") == false &&
stristr($param, "totalRows_SearchInventory") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_SearchInventory = "&" . implode("&", $newParams);
}
}
$queryString_SearchInventory = sprintf("&totalRows_SearchInventory=%d%s", $totalRows_SearchInventory, $queryString_SearchInventory);
Here is the part that generates the links for paging. I have these links in the body area of my page.
PHP:
<?php if ($pageNum_SearchInventory > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_SearchInventory=%d%s", $currentPage, 0, $queryString_SearchInventory); ?>">First</a>
<a href="<?php printf("%s?pageNum_SearchInventory=%d%s", $currentPage, max(0, $pageNum_SearchInventory - 1), $queryString_SearchInventory); ?>">Previous</a>
<?php } // Show if not first page ?>
<?php if ($pageNum_SearchInventory < $totalPages_SearchInventory) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_SearchInventory=%d%s", $currentPage, min($totalPages_SearchInventory, $pageNum_SearchInventory + 1), $queryString_SearchInventory); ?>">Next</a>
<a href="<?php printf("%s?pageNum_SearchInventory=%d%s", $currentPage, $totalPages_SearchInventory, $queryString_SearchInventory); ?>">Last</a>
<?php } // Show if not last page ?> <p>
Here is the all the PHP code above the body area of my page. You may need this to diagnose the problem.
PHP:
<?php require_once('CONNECTION SETTINGS PAGE'); ?>
<?php
$maxRows_SearchInventory = 10;
$pageNum_SearchInventory = 0;
if (isset($HTTP_GET_VARS['pageNum_SearchInventory'])) {
$pageNum_SearchInventory = $HTTP_GET_VARS['pageNum_SearchInventory'];
}
$startRow_SearchInventory = $pageNum_SearchInventory * $maxRows_SearchInventory;
mysql_select_db($database_Lighthouse, $Lighthouse);
// The basic SELECT statement
$select = 'SELECT DISTINCT *';
$from = ' FROM onlinestore';
$where = ' WHERE 1=1';
$division = $_POST['division'];
if ($division !='') { // A division is selected
$where .= " AND Category LIKE '$division'";
}
$cid = $_POST['cid'];
if ($cid != '') { // A category is selected
$from .= ', ItemLookup';
$where .= " AND name=PID AND CID='$cid'";
}
$searchtext = $_POST['searchtext'];
$searchfield = $_POST['searchfield'];
if ($searchtext != '') { // Some search text was specified
$where .= " AND $searchfield LIKE '%$searchtext%'";
}
$viewdivision = $_POST['viewdivision'];
if ($viewdivision !='') { // A division is selected
$where .= " AND Category LIKE '$viewdivision'";
}
$viewcid = $_POST['viewcid'];
if ($viewcid != '') { // A category is selected
$from .= ', ItemLookup';
$where .= " AND name=PID AND CID='$viewcid'";
}
$sidedivision = $_POST['sidedivision'];
if ($sidedivision !='') { // A division is selected
$where .= " AND Category LIKE '$sidedivision'";
}
$sidecategory = $_POST['sidecategory'];
if ($sidecategory != '') { // A category is selected
$from .= ', ItemLookup';
$where .= " AND name=PID AND CID='$sidecategory'";
}
$sidesearchtext = $_POST['sidesearchtext'];
$sidefield = $_POST['sidefield'];
if ($sidesearchtext != '') { // Some search text was specified
$where .= " AND $sidefield LIKE '%$sidesearchtext%'";
}
$order = ' ORDER BY Title ASC';
$query_SearchInventory = ($select . $from . $where.$order);
$query_limit_SearchInventory = sprintf("%s LIMIT %d, %d", $query_SearchInventory, $startRow_SearchInventory, $maxRows_SearchInventory);
$SearchInventory = mysql_query($query_limit_SearchInventory, $Lighthouse) or die(mysql_error());
$row_SearchInventory = mysql_fetch_assoc($SearchInventory);
$totalRows_SearchInventory = mysql_num_rows($SearchInventory);
if (isset($HTTP_GET_VARS['totalRows_SearchInventory'])) {
$totalRows_SearchInventory = $HTTP_GET_VARS['totalRows_SearchInventory'];
} else {
$all_SearchInventory = mysql_query($query_SearchInventory);
$totalRows_SearchInventory = mysql_num_rows($all_SearchInventory);
}
$totalPages_SearchInventory = ceil($totalRows_SearchInventory/$maxRows_SearchInventory)-1;
$queryString_SearchInventory = "";
if (!empty($HTTP_SERVER_VARS['QUERY_STRING'])) {
$params = explode("&", $HTTP_SERVER_VARS['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_SearchInventory") == false &&
stristr($param, "totalRows_SearchInventory") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_SearchInventory = "&" . implode("&", $newParams);
}
}
$queryString_SearchInventory = sprintf("&totalRows_SearchInventory=%d%s", $totalRows_SearchInventory, $queryString_SearchInventory);
?>
I just feel lost in this and don't really understand what all the code does. Please help me get this problem fixed. I would really appreciate it! If you can't help me, can you refer me to someone who can?
Thanks in advance!
Lavern Gingerich
Lighthouse Publishing, Inc.
www.lighthousepublishing.org