usman07 Posted May 8, 2012 Share Posted May 8, 2012 I have a property website and I want users to be able to filter the properties, So when they access the page, and click 'detached properties' only detached properties will appear, if the user selects 'semi-detached' then only semi detached properties will appear. I already have a mySQL database set up. Image attached here to have a better understanding. Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/ Share on other sites More sharing options...
usman07 Posted May 8, 2012 Author Share Posted May 8, 2012 HTML Code: <table border="0" cellpadding="0" cellspacing="0"> <tr> <td><div id="filter"><p class="houses" style="font-family:helvetica;color:#0155a1;font-size:14px;background:url(cutouts/forsale/filter.jpg) no-repeat;"><b><u>All</u></b> <br /> <a href="www.mumtazproperties.hostei.com/show_houses.php?type=detached"><span class="dh"><b><u>Detached Houses</u></b></span></a> <br /> <span class="dh"><b><u>Semi-detached houses</u></b></span> <br /> <span class="dh"><b><u>Terraced houses</u></b></span> <br /> <br /> <b><u>Flats / Apartments</u></b> </p></div></td> <td><div id="info1"><a href="property 2.html"><img src="cutouts/forsale/pinfo1.jpg" alt=""/></a></div></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1343998 Share on other sites More sharing options...
Loser34370 Posted May 8, 2012 Share Posted May 8, 2012 I'm not sure how you are pulling your list of houses, but if it is with a MySQL database and there is a field called "type" in your table, you can use a WHERE statement to filter your query. <a href="www.mumtazproperties.hostei.com/show_houses.php?type=detached"><span class="dh"><b><u>Detached Houses</u></b></span></a> It looks like you send a GET variable named "type" in this URL. If you store that value into a PHP variable, say $type = $_GET['type'], than you can filter your query with a WHERE statement. SELECT * from yourTBL WHERE type='$type' Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344011 Share on other sites More sharing options...
Drummin Posted May 8, 2012 Share Posted May 8, 2012 Hello usman07, this has sure been a project huh. Keep in mind that you need to retain your current search perimeters when then adding these extra filters. I am not sure if you are building an array from the first POST filter or displaying directly from the query. One way or another those values need to be saved, probably to session, then apply your extra filters. Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344019 Share on other sites More sharing options...
usman07 Posted May 8, 2012 Author Share Posted May 8, 2012 thanks loser34370(im sure ur not a loser) lol and yeah drummin, its been a long road with php. could you possibly break it down for me mate, iv not started on anything for these filters yet. Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344033 Share on other sites More sharing options...
Drummin Posted May 8, 2012 Share Posted May 8, 2012 What's your current insert.php file look like assuming this is still what you're calling your search results page. What you are going to want to do is grab the current POST values and save these to session if your display is directly from DB. If on the other hand, you are building an array from post/query that you then display, you can just filter the array based of these other values. In ether case the POST or array needs to be stored to session. However sessions are not meant to store volumes of data so saving the POST values, adding the extra filter option and re-querying the DB would be a better option. Hey just my opinion. Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344038 Share on other sites More sharing options...
usman07 Posted May 9, 2012 Author Share Posted May 9, 2012 How do i grab the current post values and save these to session from my mySQL Database? Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344282 Share on other sites More sharing options...
Drummin Posted May 10, 2012 Share Posted May 10, 2012 I don't see a copy of you current insert.php page but it started something like this. Adjust as needed. As I recall you wanted to use $_GET['type'] as your extra filter. <?php require_once 'mstr_ref.php'; function san($input){ if(get_magic_quotes_gpc()){ $input=stripcslashes($input); } $output = mysql_real_escape_string($input); return $output; } session_start(); //IF post or get type if(isset($_POST['submit']) || isset($_GET['type'])){ //if isset post set to session if (isset($_POST['submit'])){ $_SESSION['current_filter']=$_POST; $pVars = array('area'=>$_POST['areas'], 'propType'=>$_POST['prop_type'], 'saleType'=>$_POST['ptype'], 'minB'=>$_POST['min_bedrooms'], 'maxB'=>$_POST['max_bedrooms'], 'minP'=>$_POST['min_price'], 'maxP'=>$_POST['max_price']); } //if isset get type and isset session, build same array using session values adding new get type if (isset($_GET['type']) && isset($_SESSION['current_filter'])){ $pVars = array('area'=>$_SESSION['current_filter']['areas'], 'propType'=>$_SESSION['current_filter']['prop_type'], 'saleType'=>$_SESSION['current_filter']['ptype'], 'minB'=>$_SESSION['current_filter']['min_bedrooms'], 'maxB'=>$_SESSION['current_filter']['max_bedrooms'], 'minP'=>$_SESSION['current_filter']['min_price'], 'maxP'=>$_SESSION['current_filter']['max_price'], 'type'=>$_GET['type']); } //you then continued with... foreach ($pVars as $k=>$v){ $v = san($v); } $sql = new makeQuery(); $sql->manAdd('location_id', $pVars['area']); // Add the new search value if (isset($pVars['type'])){ $sql->manAdd('catagory_id', $pVars['type']); } //continue Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344391 Share on other sites More sharing options...
usman07 Posted May 10, 2012 Author Share Posted May 10, 2012 Thanks mate,il try that, where does the closing php tag go? Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344414 Share on other sites More sharing options...
usman07 Posted May 10, 2012 Author Share Posted May 10, 2012 Basically, if you check this page out: http://www.mumtazproperties.hostei.com/forsale.html It shows all my images which are links to pages, what I want it for them images to show depending on what is selected e.g. 'semi detached' then only the semi detached properties will show(that is a image). So first I need to set my mySQL table with the different types e.g. 'All' 'detached' 'semi-detached' 'terrace' and then add the directory of the images? Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344417 Share on other sites More sharing options...
Drummin Posted May 10, 2012 Share Posted May 10, 2012 So what's your query setup for getting properties forsale on this page? I see again you have an html extension, where it should php for pages that use php. It looked and sounded like you wanted a secondary filter process after the form. As this I gather is on a new page other than the search form results page then disregard my other posts I made regarding this topic. Please make sure you are clear in explaining what is happening or show code you are using. Again, as I don't see code, I will just give you some code regarding adding the filter. You've used similar coding in your other pages so you should have this down by now. You are just checking for get, making sure get is valid by comparing to allowed values, then adding the filter to your query. $sql = new makeQuery(); $ftypes=array("Detached","Semi-detached","Terraced","Flats"); if (isset($_GET['type']) && in_array("{$_GET['type']}",$ftypes)){ $sql->manAdd('type', $_GET['type']); } //Continue with rest of query and display Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344534 Share on other sites More sharing options...
usman07 Posted May 10, 2012 Author Share Posted May 10, 2012 Thanks so much for your help, i really not sure which is the best way to set it up? but Im planning to have a mysql table with the images which are links and have them display depending on the type of house it is. and yeah I need to change the page to php. This is the full HTML code for the page where I want the filter: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Mumtaz Properties</title> <link rel="stylesheet" href="cutouts/style.css"/> </head> <body> <!--Main Div Tag--> <div id="wrapper"> <div id="header"> <div id="logo"><a href="index.php"><img src="cutouts/Homepage/logo.png" alt=""/></a></div> </div> <div id="navigation"> <a id="Home" href="index.php" title="home"><span>home</span></a> <a id="Sale" href="forsale.html" title="for sale"><span>for sale</span></a> <a id="Rent" href="forrent.html" title="for rent"><span>for rent</span></a> <a id="Contact" href="contact.html" title="contact us"><span>contact us</span></a> </div> <div id="main"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td><div id="filter"><p class="houses" style="font-family:helvetica;color:#0155a1;font-size:14px;background:url(cutouts/forsale/filter.jpg) no-repeat;"><b><u>All</u></b> <br /> <span class="dh"><b><u>Detached Houses</u></b></span> <br /> <span class="dh"><b><u>Semi-detached houses</u></b></span> <br /> <span class="dh"><b><u>Terraced houses</u></b></span> <br /> <br /> <b><u>Flats / Apartments</u></b> </p></div></td> <td><div id="info1"><a href="property 2.html"><img src="cutouts/forsale/pinfo1.jpg" alt=""/></a></div></td> </tr> </table> <div id="info2"><a href="property 3.html"><img src="cutouts/forsale/pinfo2.jpg" alt=""/></a></div> <div id="info3"><a href="property 4.html"><img src="cutouts/forsale/pinfo3.jpg" alt=""/></a></div> </div> <div id="footer"> <a id="fHome" href="index.php" title="homepage"><span>homepage</span></a> <a id="fSale" href="forsale.html" title="for sale"><span>for sale</span></a> <a id="fRent" href="forrent.html" title="for rent"><span>for rent</span></a> <a id="fContact" href="contact.html" title="contact us"><span>contact us</span></a> </div> </div> </body> </html> These are the images that I want them to be displayed correctly when selecting the type of properties (<div id="info 1">"info2" and "info3" the code is just taken from the above post </p></div></td> <td><div id="info1"><a href="property 2.html"><img src="cutouts/forsale/pinfo1.jpg" alt=""/></a></div></td> </tr> </table> <div id="info2"><a href="property 3.html"><img src="cutouts/forsale/pinfo2.jpg" alt=""/></a></div> <div id="info3"><a href="property 4.html"><img src="cutouts/forsale/pinfo3.jpg" alt=""/></a></div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344536 Share on other sites More sharing options...
usman07 Posted May 10, 2012 Author Share Posted May 10, 2012 drummin the code you posted, what does that do,does it get data from the database? Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344549 Share on other sites More sharing options...
Drummin Posted May 10, 2012 Share Posted May 10, 2012 How did you end up doing your query on the results page? Basically you're doing the same thing, where your baseline "where condition" is looking for field_name='for_sale' or something along those lines. All you're going to do is add the extra filter condition AND type = '{$_GET['type']}' only I wouldn't do this directly in the query line. I would suggest getting you basic query and display section working, then we'll see about adding the filter. Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344558 Share on other sites More sharing options...
usman07 Posted May 10, 2012 Author Share Posted May 10, 2012 this is the code for the search form: <?php $locationSql = "SELECT id, area_name FROM locations GROUP by area_name"; $locQry = mysql_query($locationSql) or die (mysql_error()); echo "<select name=\"areas\" size=\"5\" style=\"width:150px;\">"; while ($location = mysql_fetch_assoc($locQry)){ echo "<option value=\"{$location['id']}\">{$location['area_name']}</option>"; } echo "</select>"; ?> </div> </td><td><p class="PT">Property type:</p></td> <td><div id="PS"> <?php $catSql = "SELECT id, cat_name FROM catagorys GROUP by cat_name"; $catQry = mysql_query($catSql) or die (mysql_error()); echo "<select name=\"prop_type\" size\"=5\" style=\"width:150px;\">"; while ($cat = mysql_fetch_assoc($catQry)){ echo "<option value=\"{$cat['id']}\">{$cat['cat_name']}</option>"; } echo "</select>"; ?> </div> </td> <td> <div id="ptype"> <input type="radio" class="styled" name="ptype" value="forsale" onclick="Toggle('Div1');" /> For Sale <p class="increase"> <input type="radio" class="styled" name="ptype" value="forrent" onclick="Toggle('Div2');" /> To Rent </p> <p class="increase"> <input type="radio" class="styled" name="ptype" value="any" onclick="Toggle('Div3');" /> Any </p> </div> </td> </tr> </table> <div id="table2"> <table id="NBtable"> <tr> <td><p class="NBS">Number of bedrooms:</p></td> <td><div id="NB"> <select name="min_bedrooms"> <?php $roomLimit = 5; for($minRooms = 0; $minRooms <= $roomLimit; $minRooms++){ if ($minRooms == 0){ echo"<option value=\"0\" selected=\"selected\">No Min</option>"; } else{ echo "<option value=\"$minRooms\">$minRooms</option>"; } } ?> </select> to <select name="max_bedrooms"> <?php $roomLimit = 5; for($maxRooms = 0; $maxRooms <= $roomLimit; $maxRooms++){ if ($maxRooms == 0){ echo"<option value=\"0\" selected=\"selected\">No Max</option>"; } else{ echo "<option value=\"$maxRooms\">$maxRooms</option>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344561 Share on other sites More sharing options...
Drummin Posted May 10, 2012 Share Posted May 10, 2012 I was talking about your results page not the form. I know you started with mysql and see you still have it in your form, but when last seen, your results page used PDO formatting. This is why I added $sql->manAdd('type', $_GET['type']); in my last post with code as I had assumed you had moved to this format. Do as I suggested making a page (in your current format) and then we can see about the filter. Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1344565 Share on other sites More sharing options...
usman07 Posted May 13, 2012 Author Share Posted May 13, 2012 Heres the code from the results page: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Mumtaz Properties</title> <link rel="stylesheet" href="cutouts/style.css"/> </head> <body> <!--Main Div Tag--> <div id="wrapperinsert"> <div id="header"> <div id="logo"><a href="index.php"><img src="cutouts/Homepage/logo.png" alt=""/></a></div> </div> <div id="navigation"> <a id="Home" href="index.php" title="home"><span>home</span></a> <a id="Sale" href="forsale.html" title="for sale"><span>for sale</span></a> <a id="Rent" href="forrent.html" title="for rent"><span>for rent</span></a> <a id="Contact" href="contact.html" title="contact us"><span>contact us</span></a> </div> <div id="main"> <div id="results"><img src="cutouts/Homepage/results.png"></img></div> <?php require_once 'mstr_ref.php'; function san($input){ if(get_magic_quotes_gpc()){ $input=stripcslashes($input); } $output = mysql_real_escape_string($input); return $output; } if(isset($_POST['submit'])){ if((isset($_POST['min_price1']) && $_POST['min_price1'] != 0) || (isset($_POST['max_price1']) && $_POST['max_price1'] != 0)){ $_POST['min_price'] = $_POST['min_price1']; $_POST['max_price'] = $_POST['max_price1']; } elseif((isset($_POST['min_price2']) && $_POST['min_price2'] != 0) || (isset($_POST['max_price2']) && $_POST['max_price2'] != 0)){ $_POST['min_price'] = $_POST['min_price2']; $_POST['max_price'] = $_POST['max_price2']; } elseif((isset($_POST['min_price3']) && $_POST['min_price3'] != 0) || (isset($_POST['max_price3']) && $_POST['max_price3'] != 0)){ $_POST['min_price'] = $_POST['min_price3']; $_POST['max_price'] = $_POST['max_price3']; } $pVars = array('area'=>$_POST['areas'], 'propType'=>$_POST['prop_type'], 'saleType'=>$_POST['ptype'], 'minB'=>$_POST['min_bedrooms'], 'maxB'=>$_POST['max_bedrooms'], 'minP'=>$_POST['min_price'], 'maxP'=>$_POST['max_price']); foreach ($pVars as $k=>$v){ $v = san($v); } $sql = new makeQuery(); $sql->manAdd('location_id', $pVars['area']); if($pVars['propType'] != 'Any'){ $sql->manAdd('catagory_id', $pVars['propType']); } if ($pVars['maxB'] > 0){ $sql->manAdd('bedrooms', $pVars['maxB'], '<='); } if($pVars['minB'] > 0){ $sql->manAdd('bedrooms',$pVars['minB'],'>='); } if($pVars['saleType'] != 'Any'){ if($pVars['saleType'] == "forsale"){ $sql->manAdd('market_type', 'sale'); if($pVars['minP'] != 0){ // $pVars['minP'] = $pVars['minP'] * 1000; } if($pVars['maxP'] != 0){ // $pVars['maxP'] = $pVars['maxP'] * 1000; } } if($pVars['saleType'] == 'forrent'){ $sql->manAdd('market_type', 'rent'); } } if ($pVars['maxP'] > 0){ $sql->manAdd('asking_price', $pVars['maxP'], '<='); } if($pVars['minP'] > 0){ $sql->manAdd('asking_price',$pVars['minP'],'>='); } $qry = $sql->sqlStart.$sql->stmt.'Group By property.id'; $results = mysql_query($qry) or die (mysql_error()."<br />|-|-|-|-|-|-|-|-|-|-|-|-<br />$qry"); if(mysql_num_rows($results) < 1){ die ('<div class="error">Sorry, No Results Match Your Search.</div>'); } while($row = mysql_fetch_assoc($results)){ echo '<div class="container" style="float:left;">'; echo '<div class="imageholder" style="float:left;">'; echo "<a href='{$row['url']}'><img class='image1' src='{$row['image_path']}' alt='{$row['summary']}' /></a> <br />"; echo '</div>'; echo '<div class="textholder" style="font-family:helvetica; font-size:14px; float:left; padding-top:10px;">'; echo "{$row['summary']}"; echo "<span style=\"color:#63be21;\"><br><br><b>{$row['bedrooms']} bedroom(s) {$row['bathrooms']} bathroom(s) {$row['receptions']} reception room(s)</b></span>"; if($row['parking'] != null){ echo "<span style=\"color:#63be21;\"><b> {$row['parking']} parking space(s)</b></span>"; echo '<div class="sline"><img src="cutouts/search/sline.png" alt=""/></div>'; } echo '</div>'; echo '<div style="clear:both"></div>'; } } else{ echo "There was a problem, please click<a href='index.php'> Here </a>to return to the main page and try again"; } ?> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1345107 Share on other sites More sharing options...
Drummin Posted May 13, 2012 Share Posted May 13, 2012 Not sure if this is even a working copy for the "forsale" page, but maybe something you can work with. Please be sure things are setup for your DB/tables/fields before trying. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Mumtaz Properties</title> <link rel="stylesheet" href="http://www.mumtazproperties.hostei.com/cutouts/style.css"/> </head> <body> <!--Main Div Tag--> <div id="wrapper"> <div id="header"> <div id="logo"><a href="index.php"><img src="http://www.mumtazproperties.hostei.com/cutouts/Homepage/logo.png" alt=""/></a></div> </div> <div id="navigation"> <a id="Home" href="index.php" title="home"><span>home</span></a> <a id="Sale" href="forsale.html" title="for sale"><span>for sale</span></a> <a id="Rent" href="forrent.html" title="for rent"><span>for rent</span></a> <a id="Contact" href="contact.html" title="contact us"><span>contact us</span></a> </div> <div id="main"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> <div id="filter"><p class="houses" style="font-family:helvetica;color:#0155a1;font-size:14px;background:url(http://www.mumtazproperties.hostei.com/cutouts/forsale/filter.jpg) no-repeat;"><a href="forsale.html"><b><u>All</u></b></a> <br /> <a href="forsale.htm?type=detached"><span class="dh"><b><u>Detached Houses</u></b></span></a> <br /> <a href="forsale.htm?type=semi-detached"><span class="dh"><b><u>Semi-detached houses</u></b></span></a> <br /> <a href="forsale.htm?type=terraced"><span class="dh"><b><u>Terraced houses</u></b></span></a> <br /> <br /> <a href="forsale.htm?type=flats"><span class="dh"><b><u>Flats / Apartments</u></b></span></a> </p></div> </td> <td> <?php require_once 'mstr_ref.php'; $sql = new makeQuery(); $sql->manAdd('market_type', 'sale'); $types=array("detached","semi-detached","terraced","flats"); if(isset($_GET['type']) && in_array($_GET['type'], $types)){ $sql->manAdd('type', "{$_GET['type']}"); } $qry = $sql->sqlStart.$sql->stmt.'Group By property.id'; $results = mysql_query($qry) or die (mysql_error()."<br />|-|-|-|-|-|-|-|-|-|-|-|-<br />$qry"); if(mysql_num_rows($results) < 1){ die ('<div class="error">Sorry, No Results Match Your Search.</div>'); } while($row = mysql_fetch_assoc($results)){ echo '<div class="container" style="float:left;">'; echo '<div class="imageholder" style="float:left;">'; echo "<a href='{$row['url']}'><img class='image1' src='{$row['image_path']}' alt='{$row['summary']}' /></a> <br />"; echo '</div>'; echo '<div class="textholder" style="font-family:helvetica; font-size:14px; float:left; padding-top:10px;">'; echo "{$row['summary']}"; echo "<span style=\"color:#63be21;\"><br><br><b>{$row['bedrooms']} bedroom(s) {$row['bathrooms']} bathroom(s) {$row['receptions']} reception room(s)</b></span>"; if($row['parking'] != null){ echo "<span style=\"color:#63be21;\"><b> {$row['parking']} parking space(s)</b></span>"; echo '<div class="sline"><img src="cutouts/search/sline.png" alt=""/></div>'; } echo '</div>'; echo '<div style="clear:both"></div>'; } ?> </td> </tr> </table> </div> <div id="footer"> <a id="fHome" href="index.php" title="homepage"><span>homepage</span></a> <a id="fSale" href="forsale.html" title="for sale"><span>for sale</span></a> <a id="fRent" href="forrent.html" title="for rent"><span>for rent</span></a> <a id="fContact" href="contact.html" title="contact us"><span>contact us</span></a> </div> </div> </body> </html> <!-- www.000webhost.com Analytics Code --> <script type="text/javascript" src="http://stats.hosting24.com/count.php"></script> <noscript><a href="http://www.hosting24.com/"><img src="http://stats.hosting24.com/count.php" alt="web hosting" /></a></noscript> <!-- End Of Analytics Code --> Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1345142 Share on other sites More sharing options...
Drummin Posted May 13, 2012 Share Posted May 13, 2012 I guess the page would have php extension, so the filter links should be changed to forsale.php?type=detached etc. Quote Link to comment https://forums.phpfreaks.com/topic/262257-how-to-create-search-filters/#findComment-1345161 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.