usman07 Posted May 16, 2012 Share Posted May 16, 2012 Basically I have a property page where I want users to be able to click 'detached' and then only detached properties will appear, if the user click 'semi-detached' then only semi-detched properties will appear. but these properties that I want to appear are just images, then a link to the actually property page with all the details. You can view the page here to get a better understanding: http://www.mumtazproperties.hostei.com/forsale.html At the moment all of the properties appear on the page. (and when you select the filters it actually does what I want it too, but through HTML) But I want the information to come from a database and then use PHP code to execute it all. Help is hugely appreciated, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/ Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 This is what i have so far: <?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>'; } ?> But I need some help on how to set up my database? Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1345983 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 Still getting used to PDO queries. See if you can make this work. Check table and field names before testing. <?php $host = "localhost"; //MySQL Database user name. $login = ""; //Password for MySQL. $dbpass = ""; //MySQL Database name. $db = ""; //Make connection to DB try { $dbh = new PDO("mysql:host=localhost;dbname=$db", $login, $dbpass); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } //Make query. Adjust table/field names as needed. //Basic query for properties for sale $sql= "SELECT * FROM property WHERE market_type='For Sale'"; //Make array of filter allowed types $types=array("detached","semi-detached","terraced","flats"); //Check for GET and if allowed type if(isset($_GET['type']) && in_array($_GET['type'], $types)){ //Add filter type to our query $sql .="type='{$_GET['type']}'"; } //Add order by $sql .=" ORDER BY id"; //execute query $result = $dbh->query($sql); //get result count $resultcount = $result->rowCount(); //check count for no results if ($resultcount < 1){ echo '<div class="error">Sorry, No Results Match Your Search.</div>'; } while($row = $result->fetch(PDO::FETCH_BOTH)){ 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346018 Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 hey mate, good to hear from you. iv replaced the code with the above that you posted and I get text displaying: 'Sorry, No Results Match Your Search.' Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346021 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 Check all the field names and variables so they match what you have. market_type='For Sale' Maybe is market_type='sale' Not sure exactly what you're using. Also it doesn't look like you've updated your search links. <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.php?type=detached"><span class="dh"><b><u>Detached Houses</u></b></span></a> <br /> <a href="forsale.php?type=semi-detached"><span class="dh"><b><u>Semi-detached houses</u></b></span></a> <br /> <a href="forsale.php?type=terraced"><span class="dh"><b><u>Terraced houses</u></b></span></a> <br /> <br /> <a href="forsale.php?type=flats"><span class="dh"><b><u>Flats / Apartments</u></b></span></a> </p></div> </td> Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346022 Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 Ahh yeah you was right i had them as 'Sale', the images don't appear on the page? Why would this be? Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346023 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 Does the field `image_path` have the correct value? Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346025 Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 Yeah it does, it just the same as the insert.php page Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346026 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 What's the URL of your test page? When viewing source can you see the image path? Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346028 Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 This is what viewing source in the browser: <img class='image1' src='' alt='Situated Heres the url to the page of the website, its the images below, the top images are in html which i'm later going to take out. URL: http://www.mumtazproperties.hostei.com/forsale.php Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346032 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 Is `image_path` the name for your field? Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346033 Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 Yeah mate it is Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346034 Share on other sites More sharing options...
Jessica Posted May 16, 2012 Share Posted May 16, 2012 After while($row = $result->fetch(PDO::FETCH_BOTH)){ add print_r($row); and look/tell us what it contains. Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346035 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 ...And from the same table `property`? Looks like you're getting $row['summary'], so it seems odd you're not getting $row['image_path']. Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346036 Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 Yeah I know, does seem strange. here what i get when adding print_r($row); Array ( [id] => 2 [0] => 2 [location_id] => 4 [1] => 4 [catagory_id] => 1 [2] => 1 [type] => House [3] => House [bedrooms] => 3 [4] => 3 [bathrooms] => 1 [5] => 1 [receptions] => 2 [6] => 2 [parking] => 4 [7] => 4 [garden] => [8] => [market_type] => Sale [9] => Sale [asking_price] => 365,000 [10] => 365,000 [pay_interval] => [11] => [summary] => Situated in Oakwood this superb three bedroom semi-detached property that simply must be viewed to be appreciated. An early inspection is most strongly recommended and in brief will reveal, front entrance porch, reception hall, utility room and a stylish living room. [12] => Situated in Oakwood this superb three bedroom semi-detached property that simply must be viewed to be appreciated. An early inspection is most strongly recommended and in brief will reveal, front entrance porch, reception hall, utility room and a stylish living room. [full_description] => [13] => => property 2.html [14] => property 2.html ) Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346037 Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 ^I dont see the images anywhere?^ Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346039 Share on other sites More sharing options...
Jessica Posted May 16, 2012 Share Posted May 16, 2012 Well, there's clearly no image_path column in there. Where is your actual SQL statement? Show that as well. Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346040 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 So table `property` doesn't hold `image_path`. What table holds this? Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346042 Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 ohh thats why? The 'catagorys' table holds image_path Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346043 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 See about modifying this section of the code. Again note I used property_id to identify table row in categorys. Change if needed. //Basic query for properies for sale $sql= "SELECT * FROM property as p "; $sql .= "LEFT JOIN catagorys AS c "; $sql .= "ON "; $sql .= "(c.property_id = p.id) "; $sql .= "WHERE p.market_type='For Sale'"; //Make array of filter allowed types $types=array("detached","semi-detached","terraced","flats"); //Check for GET and if allowed type if(isset($_GET['type']) && in_array($_GET['type'], $types)){ //Add filter type to our query $sql .="p.type='{$_GET['type']}'"; } //Add order by $sql .=" ORDER BY p.id"; Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346044 Share on other sites More sharing options...
usman07 Posted May 16, 2012 Author Share Posted May 16, 2012 I was just looking back at the insert.php page, and that doesn't have a $sql= "SELECT * FROM property WHERE market_type='Sale'"; so it doesnt select from a certain table, so if we just removed that, would that make any sense? I have absolutely no clue to what i'm saying is right or wrong. or how about using something like this: $qry = $sql->sqlStart.$sql->stmt.'Group By property.id'; $results = mysql_query($qry) or die (mysql_error()."<br />|-|-|-|-|-|-|-|-|-|-|-|-<br />$qry"); Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346046 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 Yes, sorry about that. Needs to be more like this. p.id, p.location_id, p.catagory_id, p.type, p.bedrooms, p.bathrooms, p.receptions, p.parking, p.garden, p.market_type, p.asking_price, p.pay_interval, p.summary, p.full_description, c.image_path, c.area_name Be back in 30 minutes. Hope you get it working. Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346048 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 Being used to mysql I can't imagine querying the entire DB without defining the table and field names. See if this is closer. Let me know what errors you get. Note only replaing the upper section of the query. $sql= "SELECT p.id, p.location_id, p.catagory_id, p.type, p.bedrooms, p.bathrooms, p.receptions, p.parking, p.garden, p.market_type, p.asking_price, p.pay_interval, p.url, p.summary, p.full_description, c.image_path, c.area_name FROM property as p "; $sql .= "LEFT JOIN catagorys AS c "; $sql .= "ON "; $sql .= "(c.property_id = p.id) "; $sql .= "WHERE p.market_type='For Sale'"; IF you get an error like below, a table field in the query doesn't match DB field name. Fatal error: Call to a member function rowCount() on a non-object in /home/a2221438/public_html/forsale.php on line 102 Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346070 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 Here's a full updated copy, that works on my test db. <?php $host = "localhost"; //MySQL Database user name. $login = ""; //Password for MySQL. $dbpass = ""; //MySQL Database name. $db = ""; //Make connection to DB try { $dbh = new PDO("mysql:host=localhost;dbname=$db", $login, $dbpass); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } //Make query. Adjust table/field names as needed. //Basic query for properies for sale $sql= "SELECT p.id, p.location_id, p.catagory_id, p.type, p.bedrooms, p.bathrooms, p.receptions, p.parking, p.garden, p.market_type, p.asking_price, p.pay_interval, p.url, p.summary, p.full_description, c.image_path, c.area_name FROM property as p "; $sql .= "LEFT JOIN catagorys AS c "; $sql .= "ON "; $sql .= "(c.property_id = p.id) "; $sql .= "WHERE p.market_type='sale'"; //Make array of filter allowed types $types=array("detached","semi-detached","terraced","flats"); //Check for GET and if allowed type if(isset($_GET['type']) && in_array($_GET['type'], $types)){ //Add filter type to our query $sql .="p.type='{$_GET['type']}'"; } //Add order by $sql .=" ORDER BY p.id"; //execute query $result = $dbh->query($sql); //get result count $resultcount = $result->rowCount(); //check count for no results if ($resultcount < 1){ echo '<div class="error">Sorry, No Results Match Your Search.</div>'; } while($row = $result->fetch(PDO::FETCH_BOTH)){ 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346081 Share on other sites More sharing options...
Drummin Posted May 16, 2012 Share Posted May 16, 2012 One more copy of your full page based on view source code of your page. Adjust as needed. <!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 type="text/css" 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.php" title="for sale"><span>for sale</span></a> <a id="Rent" href="forrent.php" 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.php"><b><u>All</u></b></a> <br /> <a href="forsale.php?type=detached"><span class="dh"><b><u>Detached Houses</u></b></span></a> <br /> <a href="forsale.php?type=semi-detached"><span class="dh"><b><u>Semi-detached houses</u></b></span></a> <br /> <a href="forsale.php?type=terraced"><span class="dh"><b><u>Terraced houses</u></b></span></a> <br /> <br /> <a href="forsale.php?type=flats"><span class="dh"><b><u>Flats / Apartments</u></b></span></a> </p></div> </td> <td> <?php $host = "localhost"; //MySQL Database user name. $login = ""; //Password for MySQL. $dbpass = ""; //MySQL Database name. $db = ""; //Make connection to DB try { $dbh = new PDO("mysql:host=localhost;dbname=$db", $login, $dbpass); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } //Make query. Adjust table/field names as needed. //Basic query for properies for sale $sql= "SELECT p.id, p.location_id, p.catagory_id, p.type, p.bedrooms, p.bathrooms, p.receptions, p.parking, p.garden, p.market_type, p.asking_price, p.pay_interval, p.url, p.summary, p.full_description, c.image_path, c.area_name FROM property as p "; $sql .= "LEFT JOIN catagorys AS c "; $sql .= "ON "; $sql .= "(c.property_id = p.id) "; $sql .= "WHERE p.market_type='sale'"; //Make array of filter allowed types $types=array("detached","semi-detached","terraced","flats"); //Check for GET and if allowed type if(isset($_GET['type']) && in_array($_GET['type'], $types)){ //Add filter type to our query $sql .="AND p.type='{$_GET['type']}'"; } //Add order by $sql .=" ORDER BY p.id"; //execute query $result = $dbh->query($sql); //get result count $resultcount = $result->rowCount(); //check count for no results if ($resultcount < 1){ echo '<div class="error">Sorry, No Results Match Your Search.</div>'; } while($row = $result->fetch(PDO::FETCH_BOTH)){ 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>'; echo '</div>'; } ?></td> </tr> </table> <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><!-- main --> </div><!-- wrapper --> <!-- 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 --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/262612-how-to-create-search-filters/#findComment-1346105 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.