Hi All,
I have a problem I am totally stuck on, and would appreciate help please. I’m trying to run a search script with pagination limited to 25 records per page.
The script does work apart from returning how many results your search returned.
Say I search all the records in the database (31 records overall)
On Page 1
Your search returned 25 result(s). //Wrong!
On Page 2
Your search returned 31 result(s). //Right!!!
Why does it not say 31 records on both pages? 25 on the first, then 6 on the 2nd
Any help please?
<html>
<head>
<title>Search Results ...</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="ssheet.css">
</head>
<body>
<p>Search Results</p><?php
// Start the connection to the database
include('*******');
$can_i_connect = db_connect(); // by db_connect function is in my include file
if(!$can_i_connect)
{
echo "Could not connect to database";
}
// End the connection to the database
// Start to get the data from the form and trim any whitespace
if($_SERVER["REQUEST_METHOD"]=='POST')
{
$business_type = trim($_POST['business_type']);
$town = trim($_POST['town']);
$company_name = trim($_POST['company_name']);
}
else
{
$business_type = trim($_GET['business_type']);
$town = trim($_GET['town']);
$company_name = trim($_GET['company_name']);
}
// End getting the data from the form and trimming any whitespace
// Start to build the query and order the listings by the company name
$add = '';
$search_query = "SELECT * from directory_listings where";
if($business_type == '') //Nothing entered
{
$add .= " business_type LIKE '%'";
}
else
{
$add .= " business_type = '$business_type'";
}
if(!empty($town))
{
$add .= " AND town LIKE '%$town%'";
}
if(!empty($company_name))
{
$add .= " AND company_name = '$company_name'";
}
// End building the query and order the listings by the company name
// Start pagination script and state amount of records per page
$limit = 25;
$query_count = mysql_query ( "SELECT COUNT(*) AS total FROM directory_listings WHERE " . $add );
$result_count = mysql_fetch_assoc ( $query_count );
$totalrows = $result_count['total'];
$PHP_SELF = $_SERVER['PHP_SELF'];
if( ! isset ( $_GET['page'] ) )
{
$page = 1;
}
else
{
$page = $_GET['page'];
}
$limitvalue = $page * $limit - ($limit);
// End pagination script and state amount of records per page
// Start to find how many search results are being found for the query
$search_query .= $add . " LIMIT " . $limitvalue . ", " . $limit;
$search_results = mysql_query($search_query, $can_i_connect);
$result = mysql_query($search_query) or die (mysql_error());
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT FOUND_ROWS()"), 0);
if($total_results <= 0)
{
echo "Sorry, there were no results for your search.";
}
// Else and Start to find how many pagination pages I have
else
{
echo "<b>Your search returned ".$total_results." result(s).</b> <br /><br />Here are those results, listed in ascendng order. <br /><br />";
if($page != 1){
$pageprev = $page - 1;
echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV</a> ");
}else{
echo("PREV");
}
$numofpages = $number_of_results/ $limit;
#echo "<br>", $totalrows;
#exit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page + 1;
echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");
}else{
echo("NEXT");
}
} // End of how many results I have found
mysql_free_result($result);
// End of Else and to find how many pagination pages I have
?>
<?while ($obj=mysql_fetch_object($search_results)){?>
<table width='100%' cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
<tr bgcolor="#EEEEEE">
<td height="50%" width="50%"><?echo $obj->business_name; ?></td><td height="50%" width="50%"><?echo $obj->business_type; ?></td></tr>
<tr bgcolor="#EEEEEE">
<td height="50%" width="50%"><?echo $obj->town; ?></td><td height="50%" width="50%"><a href="full_details.php?business_id=<?echo $obj->business_id; ?>"><img src="../images/more.jpg" width="80" height="19" alt="More..."></a></td></tr>
</table><br /><?}?>
</body>
</html>