Skip to main content
Known Participant
March 2, 2011
Answered

Create price range search function in php recordset

  • March 2, 2011
  • 1 reply
  • 6502 views

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

This topic has been closed for replies.
Correct answer Günter_Schenk

Ok, have updated:

<?php require_once('Connections/test.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  if (PHP_VERSION < 6) {

    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  }

  $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;

}

}

$colname_Recordset1 = "-1";

if (isset($_POST['prop_type'])) {

  $colname_Recordset1 = $_POST['prop_type'];

}

mysql_select_db($database_test, $test);

$query_Recordset1 = sprintf("SELECT * FROM images WHERE prop_type = %s", GetSQLValueString($colname_Recordset1, "text"));

$Recordset1 = mysql_query($query_Recordset1, $test) or die(mysql_error());

$row_Recordset1 = mysql_fetch_assoc($Recordset1);

$totalRows_Recordset1 = mysql_num_rows($Recordset1);

?>

<form action="results.php" method="post" name="search" id="search" >

        <table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">

          <tr>

            <td>Property Type </td>

            <td><select name="prop_type" id="prop_type">

                <option value="sales">Residential Sales</option>

                <option value="lettings">Residential Lettings</option>

                </select></td>

          </tr>

          <tr>

            <td>Min Price </td>

            <td><select name="prop_price" id="prop_price">

            <option value="Minimum">Minimum</option>

            <option value="250000">£250,000</option>

            <option value="500000">£500,000</option>

            <option value="1000000">£1,000,000</option>

            <option value="2000000">£2,000,000</option>

            <option value="5000000">£5,000,000</option>

            </select></td>

          </tr>

          <tr>

            <td>Max Price </td>

            <td><select name="prop_price" id="prop_price">

            <option value="Maximum">Maximum</option>

            <option value="250000">£250,000</option>

            <option value="500000">£500,000</option>

            <option value="1000000">£1,000,000</option>

            <option value="2000000">£2,000,000</option>

            <option value="5000000">£5,000,000</option>

            </select></td>

          </tr>

          <tr>

          <td>Bedrooms </td>

            <td><select name="beds" id="beds">

            <option value="0">Studio</option>

            <option value="1">1</option>

            <option value="2">2</option>

            <option value="3">3</option>

            <option value="4">4</option>

            <option value=">4">5+</option>          

            </select></td>

          </tr>

          <tr>

          <td>Location </td>

            <td><select name="prop_location" id="prop_location">

            <option value="AllLondon">All London Boroughs</option>

            <option value="NorthLondon">North London</option>

            <option value="CentralLondon">Central London</option>

            <option value="SouthWestLondon">South West London</option>

            <option value="SouthEastLondon">South East London</option>

            </select></td>

          </tr>

        </table>

        <p align="center">

          <input name="search" type="submit" id="search" value="search">

        </p>

      </form>

        </table>

        <p align="center">

          <input name="search" type="submit" id="search" value="search">

        </p>

      </form>

<?php

mysql_free_result($Recordset1);

?>

Thanks again


JackND wrote:

Ok, have updated

So does the current Recordset work or not ?

Please fix the following form elements as well:

         <tr>

            <td>Min Price </td>

            <td><select name="prop_price" id="prop_price">

            <option value="Minimum">Minimum</option>

            <option value="250000">£250,000</option>

            <option value="500000">£500,000</option>

            <option value="1000000">£1,000,000</option>

            <option value="2000000">£2,000,000</option>

            <option value="5000000">£5,000,000</option>

            </select></td>

          </tr>

          <tr>

            <td>Max Price </td>

            <td><select name="prop_price" id="prop_price">

            <option value="Maximum">Maximum</option>

            <option value="250000">£250,000</option>

            <option value="500000">£500,000</option>

            <option value="1000000">£1,000,000</option>

            <option value="2000000">£2,000,000</option>

            <option value="5000000">£5,000,000</option>

            </select></td>

          </tr>

you can´t use the same name/id value with multiple form elements, because the Recordset query needs unique $_POST variable names. Please rename the name/id values like this:

<td>Min Price </td>

<td><select name="prop_price_min" id="prop_price_min">

<td>Max Price </td>

<td><select name="prop_price_max" id="prop_price_max">

In addition to this it would rather make sense to define an empty value for the very first option, means:

<option value="">Minimum</option>

<option value="">Maximum</option>

          <tr>

          <td>Bedrooms </td>

            <td><select name="beds" id="beds">

            <option value="0">Studio</option>

            <option value="1">1</option>

            <option value="2">2</option>

            <option value="3">3</option>

            <option value="4">4</option>

            <option value=">4">5+</option>          

            </select></td>

          </tr>

1. When defining a "Studio" property, are you actually storing the bedroom value 0 in the database ? If yes, it´s alright, but if it´s a different value (or even no value at all, aka NULL) please let me know.

2. As mentioned in a previous post, you shouldn´t use comparison operators in conjunction with integer option values. Please replace the value ">4" with "5", what will be all you need for getting this segment of our conditional query properly defined.

1 reply

Günter_Schenk
Inspiring
March 3, 2011

Jack,

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.

first things first: you can´t use comparison operators such as > or < as part of a numeric value you´re passing to a query, because...

a) those operators need to be defined in the query itself

b) they need to precede the transfered value, which in turn...

c) has to be strictly numeric for having MySQL perform some calculations

Essentially, I need to incorporate the WHERE function into my sql query

In your case the WHERE clause can´t be defined statically any longer, because both the comparison operators plus the values will vary, meaning that the syntax of the WHERE - condition will vary as well.

That said, the WHERE clause need to become dynamic, and this can be done by...

a) have the following code precede the current query:

if(isset($_POST['prop_price']))

{

switch($_POST['prop_price'])

{

  case ("a"):

$price_whereclause = ' WHERE prop_price < 1000000 ';

  break;

  case ("b"):

$price_whereclause  = ' WHERE prop_price BETWEEN 100000 AND 200000 ';

  break;

  case ("c"):

$price_whereclause  = ' WHERE prop_price < 2000000 ';

  break;

}

}

else {

$price_whereclause  = '';

}

b) as the custom PHP variable $price_whereclause provides a predefined set of different WHERE clauses (or none at all), it´s this variable you need to incorporate into the query, example:

$query_getDetails = "SELECT id, prop_name, prop_price, country, post_code, short_desc, image FROM images ".$price_whereclause." order by prop_price DESC";

You might wonder what

- case ("a")

- case ("b")

- case ("c")

is all about. These are the OPTION values you´re now passing to the query

<select name="prop_price" id="prop_price">

<option value="a">&lt;£1,000,000</option>

<option value="b">£1,000,000 - £2,000,000</option>

<option value="c">&gt;£2,000,000</option>

</select>

Known Participant
March 3, 2011

Hi Gunter,

Thanks for your quick response. I thought I had it but not quite yet. I uploaded these pages into live browser but am only getting the following on the search results page:

This is the case for all three parameters. As I have not yet uploaded all the properties into mysql, there are only 2 live ones in there, one is prop_price at 600,000 (int) and the other prop_price 2300000 (int). For the query to work, should this field in mysql be a different format?

For your reference, the new show_results.php code is:

<?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;


if(isset($_POST['prop_price']))

{

switch($_POST['prop_price'])

{

  case ("a"):

$price_whereclause = ' WHERE prop_price < 1000000 ';

  break;

  case ("b"):

$price_whereclause  = ' WHERE prop_price BETWEEN 100000 AND 200000 ';

  break;

  case ("c"):

$price_whereclause  = ' WHERE prop_price < 2000000 ';

  break;

}

}

else {

$price_whereclause  = '';

}

mysql_select_db($database_test, $test);

$query_getDetails = "SELECT id, prop_name, prop_price, country, post_code, short_desc, image FROM images ".$price_whereclause." 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);

?>

I know its very cheeky, but assuming you can help with fixing this, where in the coding would I insert a "No properties returned. Please try another query" response should the db not have anything in the set parameters (instead of the symbol currently being displayed).

Again thanks so much for all your help.

Jack

Known Participant
March 3, 2011

HI Gunter,

Had another play around with it today and changed the sql slightly, instead of:

$query_getDetails = "SELECT id, prop_name, prop_price, country, post_code, short_desc, image FROM images ".$price_whereclause." order by prop_price DESC";

I have:

$query_getDetails = "SELECT id, prop_name, prop_price, country, post_code, short_desc, image FROM images $price_whereclause order by prop_price DESC";

and all working beautifully. Thank you so much! Finally as I mentioned in my last post, where would I put a "No results" field into the coding for it to display should there be no rows returned from the db....at the moment, all I am seeing is the blue question mark logo (posted yesterday) when nothing is retrieved......

Thanks again!

Jack