Hi Guys,
On my search page I built yesterday I got it working fine, and today i'm trying to add pagination to it (which is quite hard to get your head round!):
However I want to add pagination to this search results page, so I’ve put together this page to add pagination with my search results:PHP Code:<html>
<head>
<title>Search Results ...</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../search/ssheet.css">
</head>
<body>
<p>Search Results</p><?php
// 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
$search_query = "select * from directory_listings where ";
if($business_type == '') //Nothing entered
{
$search_query .= " business_type LIKE '%'";
}
else
{
$search_query .= " business_type = '$business_type'";
}
if(!empty($town))
{
$search_query .= " AND town LIKE '%$town%'";
}
if(!empty($company_name))
{
$search_query .= " AND company_name = '$company_name'";
}
$search_query .= " order by company_name asc";
// End building the query and order the listings by the company name
// 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 find how many search results are being found for the query
$search_results = mysql_query($search_query, $can_i_connect);
$result = mysql_query($search_query) or die (mysql_error());
$number_of_results = mysql_num_rows($search_results);
if($number_of_results <= 0)
{
echo "Sorry, there were no results for your search in the Fife & Kinross Online Edition.";
}
else
{
echo "<b>Your search returned ".$number_of_results." result(s).</b> <br /><br />Here are those results, listed in ascendng order. <br /><br />";
}
// End to find how many search results are being found for the query
?>
<?while ($obj=mysql_fetch_object($search_results)){?>
<table width='100%'>
<tr>
<td height="26"><?echo $obj->business_name; ?></td><td height="26"><?echo $obj->business_type; ?></td></tr>
<tr>
<td height="28"><?echo $obj->town; ?></td><td height="28"><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><?}?>
</body>
</html>
This however:PHP Code:<html>
<head>
<title>Search Results ...</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../search/ssheet.css">
</head>
<body>
<p>Search Results</p>
<?php
// 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
$search_query = "select * from directory_listings where ";
if($business_type == '') //Nothing entered
{
$search_query .= " business_type LIKE '%'";
}
else
{
$search_query .= " business_type = '$business_type'";
}
if(!empty($town))
{
$search_query .= " AND town LIKE '%$town%'";
}
if(!empty($company_name))
{
$search_query .= " AND company_name = '$company_name'";
}
$search_query .= " order by company_name asc";
// End building the query and order the listings by the company name
// 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 find how many search results are being found for the query
$search_results = mysql_query($search_query, $can_i_connect);
$result = mysql_query($search_query) or die (mysql_error());
$number_of_results = mysql_num_rows($search_results);
if($number_of_results <= 0)
{
echo "Sorry, there were no results for your search in the Fife & Kinross Online Edition.";
}
else
{
echo "<b>Your search returned ".$number_of_results." result(s).</b> <br /><br />Here are those results, listed in ascendng order. <br /><br />";
}
// End to find how many search results are being found for the query
// Start the pagination
$limit = 25;
$query_count = "SELECT * FROM directory_listings";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
$PHP_SELF = $_SERVER['PHP_SELF'];
if(!isset($_GET['page'])){
$page = 1;
} else{
$page = $_GET['page'];
}
$limitvalue = $page * $limit - ($limit);
// Ex: (page2 * 5(items per page) = 10) - 5 = 5 <- data starts at 5
if($page != 1){
$pageprev = $page - 1;
echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV</a> ");
}else{
echo("PREV");
}
$numofpages = $totalrows / $limit;
#echo "<br>", $totalrows;
#exit;
for($i = 1; $i <= $numofpages; $i++){
than $numofpages. */
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");
}
mysql_free_result($result);
// End Pagination
?>
<?while ($obj=mysql_fetch_object($search_results)){?>
<table width='100%'>
<tr>
<td height="26"><?echo $obj->business_name; ?></td><td height="26"><?echo $obj->business_type; ?></td></tr>
<tr>
<td height="28"><?echo $obj->town; ?></td><td height="28"><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><?}?>
</body>
</html>
- Returns the correct result(s) I searched for
- Then on the bottom has a pagination BUT when I click on the next page of the pagination it then displays ALL the results in my database.
Can anyone please help?
Thanks
Chris




and just wrote the minimum code to show you how to use it)).
Bookmarks