Jump to content

How to create search filters?


usman07

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 )

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.