Create price range search function in php recordset
Hi,
On a continuation from a previous thread, I am finding that the expertise of the web designers on these forums is proving much more educational and beneficial than the vast numbers of books I have bought to setup a website for my property company. (Without sounding too sycophantic!).
I have managed to create a working website where the properties in my database and their related images are pulled into a recordset on the live webpage and everything is working perfectly. I do however want to add some simple filters on the search page to narrow down the results from the database.
I am finding that given one push or piece of coding I can work out the rest myself. Therefore, all I would like to do is create a price range drop down, where clients can select properties from the database in one of 3 categories: under £1m, £1m-£2m, and over £2m.
As I understand it, I need two pages to do this, one html form page where the clients will input their search criteria with coding similar to:
<form action="search_results.php" method="post" name="search" id="search" >
Price Range:
<select name="prop_price" id="select8">
<option value="<1000000"> >£1,000,000 </option>
<option value="1000000-2000000"> £1,000,000 - £2,000,000</option>
<option value=">2000000"> >£2,000,000 </option>
</select>
<input name="search" type="submit" id="search" value="search">
</form>
(apologies for my messy coding - I am still new and not sure if certain fields arent required yet!).
Then, I would need to create the search_results.php page which would give the results of properties in the database depending on the price range submitted. I have tried various different methods put online through the forums, but cannot seem to get anything post 2006, nor anything that has actually worked. I am using Dreamweaver CS5 and MySQL for my db.
How do I incorporate the above search criteria into a dynamic php doc? The only way I have managed to get something working is to create a new page for each of the price variables -something which is incredibly inefficient, and hardly something that can be developed easily going forward.
The php code I currently have that is retrieving all the properties from my database is as follows (connection name is test, recordset name is getDetails):
<?php require_once('Connections/test.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$maxRows_getDetails = 20;
$pageNum_getDetails = 0;
if (isset($_GET['pageNum_getDetails'])) {
$pageNum_getDetails = $_GET['pageNum_getDetails'];
}
$startRow_getDetails = $pageNum_getDetails * $maxRows_getDetails;
mysql_select_db($database_test, $test);
$query_getDetails = "SELECT id, prop_name, prop_price, country, post_code, short_desc, image FROM images order by prop_price DESC";
$query_limit_getDetails = sprintf("%s LIMIT %d, %d", $query_getDetails, $startRow_getDetails, $maxRows_getDetails);
$getDetails = mysql_query($query_limit_getDetails, $test) or die(mysql_error());
$row_getDetails = mysql_fetch_assoc($getDetails);
if (isset($_GET['totalRows_getDetails'])) {
$totalRows_getDetails = $_GET['totalRows_getDetails'];
} else {
$all_getDetails = mysql_query($query_getDetails);
$totalRows_getDetails = mysql_num_rows($all_getDetails);
}
$totalPages_getDetails = ceil($totalRows_getDetails/$maxRows_getDetails)-1;
?>
<table width="990" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
</tr>
<?php do { ?>
<tr>
<td><img src="show_image.php?id=<?php echo $row_getDetails['id']; ?>"></td>
<td><?php echo $row_getDetails['prop_name']; ?></td>
<td><?php echo $row_getDetails['prop_price']; ?></td>
<td><?php echo $row_getDetails['short_desc']; ?></td>
</tr>
<?php } while ($row_getDetails = mysql_fetch_assoc($getDetails)); ?>
</table>
<?php
mysql_free_result($getDetails);
?>
Essentially, I need to incorporate the WHERE function into my sql query, but how do I do this so that it is dependent on the clients submission on the search page.......?
Sorry if this is incredibly easy, but I am at a complete loss now and cant try any more techniques from the various forums out there! I even downloaded the entire Zen cart framework just to see if I could find something in the vast numbers of php pages that came with it!
Hope someone can help -
Thanks
Jack