Link to home
Start Free TrialLog in
Avatar of satmanuk
satmanukFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help with search using check boxes - PHP MYSQL

Hi all,

I am working on a site using PHP and mysql.

I have a page where records are being displayed (max 10 per page). The page lists houses with various details on each house.

I want to make a search feature on the side of the page where i can search for houses at certain price ranges. I have added 5 check boxes and 2 tick boxes in a form but i am not sure what the best way is to go about integrating the search with my existing code.
Here how i plan to use the check boxes:
Check box 1 - Show all from table
Check box 2 - Show all below 150,000
Check box 3 - Show all betwenn 150,000 and 250,000
Check box 4 Show all between 250,000 and 350,000
Check box 5 show all over 350,000

I also want to give the option to only show new houses and the option to not show Sold houses.

I am looking to get some advice how best to integrate this idea.

My code:

<?php require_once('Connections/EE_con.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_EE_con, $EE_con);
$query_Recordset1 = "SELECT * FROM properties";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $EE_con) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>For Sale</title>
<style type="text/css" media="screen">
<!--
@import url("p7pm/p7pmh14.css");
@import url("bartstyles/bart2colfluidh.css");
-->
</style>
<!--[if lte IE 6]>
<style type="text/css">
a {height: 1em;}
#pagewrapper{
width:expression(parseInt(this.currentStyle.marginLeft)+parseInt(this.currentStyle.marginRight)-document.body.clientWidth>=-720?"720px":"auto");}
</style>
<![endif]-->
<!--[if IE 5]>
<style type="text/css">
#copyrightdiv {margin-top: -20px;}
</style>
<![endif]-->
</head>

<body>
<div id="pagewrapper">

<div id="masthead">
<div id="logodiv">
<img src="images/barthz_logo.jpg" alt="Bartlett - Established 1884 in New York City" width="273" height="130"></div>
</div>
<div id="menubar">
<ul id="p7PMnav">
<li><a href="aboutus.php">About Us </a></li>
<li><a href="forsale.php">Property For Sale </a></li>
<li><a href="forrent.php">Property For Rent </a></li>
<li><a href="contactus.php">Contact Us </a></li>
</ul>
<div class="clearmenu">&nbsp;</div>
</div>
<div id="mainbox">
<div id="sidebar">
<h3 class="top"><a href="admin.php">Admin</a></h3>
<form name="form1" method="post" action="">
  <p>&nbsp;    </p>
  <p>
    <input name="radiobutton" type="radio" value="radiobutton">
    <strong>All Properties   </strong></p>
  <p>
    <input name="radiobutton" type="radio" value="radiobutton">
    <strong>up to &pound;150,000 </strong></p>
  <p>
    <input name="radiobutton" type="radio" value="radiobutton">
    <strong>&pound;150,000 - &pound;250,000</strong> </p>
  <p>
    <input name="radiobutton" type="radio" value="radiobutton">
    <strong>&pound;250,000 - &pound;350,000</strong> </p>
  <p>
    <input name="radiobutton" type="radio" value="radiobutton">
    <strong>&pound;350,000 and above </strong></p>
  <p>
    <input type="checkbox" name="checkbox" value="checkbox">
    <strong>  Exclude Sold properties </strong></p>
  <p>
    <input type="checkbox" name="checkbox2" value="checkbox">
    <strong>New Properties Only </strong></p>
  <p>
    <input type="submit" name="Submit" value="Search">
</p>
</form>
<p>&nbsp;</p>
</div>
<div id="maincontent">
<h1>Records <?php echo ($startRow_Recordset1 + 1) ?> to <?php echo min($startRow_Recordset1 + $maxRows_Recordset1, $totalRows_Recordset1) ?> of <?php echo $totalRows_Recordset1 ?></h1>
<p align="center"><a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a>&nbsp;<a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a></p>
<table width="500" border="0" align="center">
 
  <?php do { ?>
   
    <tr>
      <td rowspan="2"><a href="detail.php?recordID=<?php echo $row_Recordset1['propertyid']; ?>"><img src="<?php echo $row_Recordset1['pic1']; ?>" width="180" height="180" border="0" /></a></td>
      <td><?php echo $row_Recordset1['propertyname']; ?></td>
    </tr>
    <tr>
      <td><?php echo $row_Recordset1['propertydesc']; ?></td>
    </tr>
    <tr>
      <td><a href="update.php?propertyid=<?php echo $row_Recordset1['propertyid']; ?>">Update</a></td>
      <td><a href="confirmDelete.php?propertyid=<?php echo $row_Recordset1['propertyid']; ?>">Delete</a></td>
        

    </tr>
    <tr>
      <td>&nbsp; <a href="detail.php?recordID=<?php echo $row_Recordset1['propertyid']; ?>"><?php echo $row_Recordset1['prop_detail']; ?></a></td>
      <td>&pound;<?php echo $row_Recordset1['price']; ?></td>
    </tr>
    <tr>
      <td colspan="2"><a href="detail.php?recordID=<?php echo $row_Recordset1['propertyid']; ?>"></a>_________________________________________________________</td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<br>
<table border="0" width="50%" align="center">
  <tr>
    <td width="23%" align="center"><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>">First</a>
          <?php } // Show if not first page ?>
    </td>
    <td width="31%" align="center"><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a>
          <?php } // Show if not first page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a>
          <?php } // Show if not last page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>">Last</a>
          <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p></p>
</div>

</div>

<div id="copyrightdiv">
<div id="copyrightnotice">
<img src="images/barthz_copyright.jpg" alt="Copyright 2005" width="292" height="26"></div>
</div>
<div id="footer">
<p>Blah, LTD. Phone: 0800 000000 Fax: 0800 000000 </p>
<p>Contact Information | Press Releases</p>
</div>

</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);


?>

Hope you can help

TIA
ASKER CERTIFIED SOLUTION
Avatar of mankowitz
mankowitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of satmanuk

ASKER

Thanks for your help.

I am missing something as it isnt working as yet. I sorted the wrong spelling in the where clause and i gave the values a unique name on the form. I also changed cost to price as this matches the field name in the database. I added forsale.php to the action on the form as i belive this needs to be set the same as the page running the search
I have left the check boxes out of the query for now.

What have i missed?


I changed my code here:
mysql_select_db($database_EE_con, $EE_con);
if (isset($_GET['Radio1'])) {
   $where_clause = "";
} elseif (isset($_GET['Radio2'])) {
   $where_clause = "WHERE price < 150000";
} elseif (isset($_GET['Radio3'])) {
   $where_clause = "WHERE price BETWEEN 150000 AND 250000";
} elseif (isset($_GET['Radio4'])) {
   $where_clause = "WHERE price BETWEEN 250000 AND 350000";
} elseif (isset($_GET['Radio5'])) {
   $where_clause = "WHERE price > 350000";
}

$query_Recordset1 = "SELECT * FROM properties" . $where_clause;
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);

and here:

<form name="form1" method="post" action="forsale.php">
  <p>&nbsp;    </p>
  <p>
    <input name="radiobutton1" type="radio" value="Radio1">
    <strong>All Properties   </strong></p>
  <p>
    <input name="radiobutton2" type="radio" value="Radio2">
    <strong>up to &pound;150,000 </strong></p>
  <p>
    <input name="radiobutton3" type="radio" value="Radio3">
    <strong>&pound;150,000 - &pound;250,000</strong> </p>
  <p>
    <input name="radiobutton4" type="radio" value="Radio4">
    <strong>&pound;250,000 - &pound;350,000</strong> </p>
  <p>
    <input name="radiobutton5" type="radio" value="Radio5">
    <strong>&pound;350,000 and above </strong></p>
  <p>
    <input type="checkbox" name="checkbox" value="checkbox">
    <strong>  Exclude Sold properties </strong></p>
  <p>
    <input type="checkbox" name="checkbox2" value="checkbox">
    <strong>New Properties Only </strong></p>
  <p>
    <input type="submit" name="Submit" value="Search">
</p>
</form>
You know what? I think I may have misled you. Check out the format for html radio buttons at http://www.echoecho.com/htmlforms10.htm

The NAME for all the radio buttons should be the same, but the VALUE should be different...

<input name="priceradio" type="radio" value="Radio1">
    <strong>All Properties   </strong></p>
  <p>
    <input name="priceradio" type="radio" value="Radio2">
    <strong>up to &pound;150,000 </strong></p>
  <p>
    <input name="priceradio" type="radio" value="Radio3">
    <strong>&pound;150,000 - &pound;250,000</strong> </p>
  <p>
    <input name="priceradio" type="radio" value="Radio4">
    <strong>&pound;250,000 - &pound;350,000</strong> </p>
  <p>
    <input name="priceradio" type="radio" value="Radio5">
    <strong>&pound;350,000 and above </strong></p>


And then in your php,
if ($_GET['radioprice']=="radio1")) {
   $where_clause = "";
} elseif ($_GET['radioprice']=="radio2")) {
   $where_clause = "WHERE price < 150000";
} elseif ($_GET['radioprice']=="radio3")) {
   $where_clause = "WHERE price BETWEEN 150000 AND 250000";
} elseif ($_GET['radioprice']=="radio4")) {
   $where_clause = "WHERE price BETWEEN 250000 AND 350000";
} elseif ($_GET['radioprice']=="radio5")) {
   $where_clause = "WHERE price > 350000";

Ok i tried that. Still doesnt work? i tried to echo out the where_clause but it shows nothing..

Any Ideas?

<?php require_once('Connections/EE_con.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_EE_con, $EE_con);


if ($_GET['radioprice']=="radio1") {
   $where_clause = "";
} elseif ($_GET['radioprice']=="radio2") {
   $where_clause = "WHERE price < 150000";
} elseif ($_GET['radioprice']=="radio3") {
   $where_clause = "WHERE price BETWEEN 150000 AND 250000";
} elseif ($_GET['radioprice']=="radio4") {
   $where_clause = "WHERE price BETWEEN 250000 AND 350000";
} elseif ($_GET['radioprice']=="radio5") {
   $where_clause = "WHERE price > 350000";
   }


$query_Recordset1 = "SELECT * FROM properties" . $where_clause;
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $EE_con) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>For Sale</title>
<style type="text/css" media="screen">
<!--
@import url("p7pm/p7pmh14.css");
@import url("bartstyles/bart2colfluidh.css");
-->
</style>
<!--[if lte IE 6]>
<style type="text/css">
a {height: 1em;}
#pagewrapper{
width:expression(parseInt(this.currentStyle.marginLeft)+parseInt(this.currentStyle.marginRight)-document.body.clientWidth>=-720?"720px":"auto");}
</style>
<![endif]-->
<!--[if IE 5]>
<style type="text/css">
#copyrightdiv {margin-top: -20px;}
</style>
<![endif]-->
</head>

<body>
<div id="pagewrapper">

<div id="masthead">
<div id="logodiv">
<img src="images/barthz_logo.jpg" alt="Bartlett - Established 1884 in New York City" width="273" height="130"></div>
</div>
<div id="menubar">
<ul id="p7PMnav">
<li><a href="aboutus.php">About Us </a></li>
<li><a href="forsale.php">Property For Sale </a></li>
<li><a href="forrent.php">Property For Rent </a></li>
<li><a href="contactus.php">Contact Us </a></li>
</ul>
<div class="clearmenu">&nbsp;</div>
</div>
<div id="mainbox">
<div id="sidebar">
<h3 class="top"><a href="admin.php">Admin</a></h3>
<form name="form1" method="post" action="forsale.php">
  <p>&nbsp;    </p>
  <p>
    <input name="radioprice" type="radio" value="radio1" checked />
    <strong>All Properties   </strong></p>
  <p>
    <input name="radioprice" type="radio" value="radio2" />
    <strong>up to &pound;150,000 </strong></p>
  <p>
    <input name="radioprice" type="radio" value="radio3" />
    <strong>&pound;150,000 - &pound;250,000</strong> </p>
  <p>
    <input name="radioprice" type="radio" value="radio4" />
    <strong>&pound;250,000 - &pound;350,000</strong> </p>
  <p>
    <input name="radioprice" type="radio" value="radio5" />
    <strong>&pound;350,000 and above </strong></p>
  <p>&nbsp;</p>
  <p>
    <input type="submit" name="submit" value="Search" />
</p>
</form>
<p>&nbsp;</p>
</div>
<div id="maincontent">
<h1>Records <?php echo ($startRow_Recordset1 + 1) ?> to <?php echo min($startRow_Recordset1 + $maxRows_Recordset1, $totalRows_Recordset1) ?> of <?php echo $totalRows_Recordset1 ?></h1>
<p align="center"><a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a>&nbsp;<a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a></p>
<table width="500" border="0" align="center">
 
  <?php do { ?>
   
    <tr>
      <td rowspan="2"><a href="detail.php?recordID=<?php echo $row_Recordset1['propertyid']; ?>"><img src="<?php echo $row_Recordset1['pic1']; ?>" width="180" height="180" border="0" /></a></td>
      <td><?php echo $row_Recordset1['propertyname']; ?></td>
    </tr>
    <tr>
      <td><?php echo $row_Recordset1['propertydesc']; ?></td>
    </tr>
    <tr>
      <td><a href="update.php?propertyid=<?php echo $row_Recordset1['propertyid']; ?>">Update</a></td>
      <td><a href="confirmDelete.php?propertyid=<?php echo $row_Recordset1['propertyid']; ?>">Delete</a></td>
        

    </tr>
    <tr>
      <td>&nbsp; <a href="detail.php?recordID=<?php echo $row_Recordset1['propertyid']; ?>"><?php echo $row_Recordset1['prop_detail']; ?></a></td>
      <td>&pound;<?php echo $row_Recordset1['price']; ?></td>
    </tr>
    <tr>
      <td colspan="2"><a href="detail.php?recordID=<?php echo $row_Recordset1['propertyid']; ?>"></a>_________________________________________________________</td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<br>
<table border="0" width="50%" align="center">
  <tr>
    <td width="23%" align="center"><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>">First</a>
          <?php } // Show if not first page ?>
    </td>
    <td width="31%" align="center"><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>">Previous</a>
          <?php } // Show if not first page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>">Next</a>
          <?php } // Show if not last page ?>
    </td>
    <td width="23%" align="center"><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>">Last</a>
          <?php } // Show if not last page ?>
    </td>
  </tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p></p>
</div>

</div>

<div id="copyrightdiv">
<div id="copyrightnotice">
<img src="images/barthz_copyright.jpg" alt="Copyright 2005 Project Seven Development" width="292" height="26"></div>
</div>
<div id="footer">
<p>Baynham , LTD. Phone: 0800 000000 Fax: 0800 000000 </p>
<p>Contact Information | Press Releases</p>
</div>

</div>
</body>
</html>
<?php
mysql_free_result($Recordset1);


?>
hmmm. ok, try echoing out radioprice... is it getting that? Probably not. I see that you have method=post, so you probably have to do $_POST['radioprice'] instead of $_GET
Cool, that sorted it

Thanks