Advertisement

05.03.2008 at 11:41PM PDT, ID: 23374611
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.6

PHP Search Query for Real Estate Search Box

Asked by jenem in PHP Scripting Language, MySQL Server

Tags:

Hi there Experts
Hope someone can assist with this.
I am using a script for a Real Estate site.  The script contains code for a search box with multiple options to select from, i.e. price, bedrooms, city, etc.
It seems to work okay except that I would like the option to search by any categories, types or locations.
I'm not quite sure how to do this - hopefully it is simple to change the existing search function - (snippet attached).  The fields are listingType, listingCategory and listingCity.

Thanks in advance - as always I appreciate any assistance.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
function searchListings($pageNo,$listAmount,$keyword,$queryType)
	{
	if($queryType == "adv")//do an 'advanced' search
		{
		$limit = $listAmount;
		if(!isset($_GET['page']))
			{
			$page = 1;
			}else
				{
				$page = $_GET['page'];
				}
		$offset = ($page -1) * $limit;		
		$advSql = "";
		//////////////////build query//////////////////////////
		if($_POST['priceLow'] > 0)
			{
			$advSql .= " and ( ListingsMain.listingPrice >= '".$_POST['priceLow']."' )";
			}
		if($_POST['priceHigh'] > 0)
			{
			$advSql .= " and ( ListingsMain.listingPrice BETWEEN '".$_POST['priceLow']."' AND  '".$_POST['priceHigh']."' ) ";
			}			
		if($_POST['bedroomsHigh'] > 0)
			{
			$advSql .= " and ( bedrooms BETWEEN '".$_POST['bedrooms']."' AND '".$_POST['bedroomsHigh']."' )";
			}elseif($_POST['bedrooms'] > 0)
				{
				$advSql .= " and ( bedrooms >= '".$_POST['bedrooms']."' )";
				}
		if($_POST['bathsHigh'] > 0)
			{
			$advSql .= " and ( totalBaths BETWEEN '".$_POST['baths']."' AND '".$_POST['bathsHigh']."' )";
			}elseif($_POST['baths'] > 0)
				{
				$advSql .= " and ( totalBaths >= '".$_POST['baths']."' )";
				}	
		
		if(!empty($_POST['city']))
			{
			$advSql .= " and ( listingCity LIKE '".$_POST['city']."' )";
			}	
 
		if(!empty($_POST['propertyType']))
			{
			$advSql .= " and ( listingType LIKE '".$_POST['propertyType']."' )";
			}	
		if(!empty($_POST['categoryCode']))
			{
			$advSql .= " and ( listingCategory LIKE '".$_POST['categoryCode']."' )";
			}						
							
		$query = "SELECT ListingsMain.*,ListingDimData.ID,ListingDimData.bedrooms FROM ListingsMain LEFT JOIN ListingDimData ON ListingsMain.ID=ListingDimData.ID WHERE ListingsMain.listingStatus = 'Current' ".$advSql." LIMIT  ".$offset.",".$limit." ";
		$sqlPage = mysql_query("SELECT ListingsMain.*,ListingDimData.ID,ListingDimData.bedrooms FROM ListingsMain LEFT JOIN ListingDimData ON ListingsMain.ID=ListingDimData.ID WHERE ListingsMain.listingStatus = 'Current' ".$advSql."") or die( mysql_error() );
		
		///////////////////////////////////////////////////////		
		$searchQuery = mysql_query($query);	
		$objects = new Create;
		$photos = new Photos;	
		while($rows = mysql_fetch_assoc($searchQuery))
			{
			$str = file_get_contents(SCRIPT_PATH."/templates/listingbox.php"); //Grab the template file		
			$str = str_replace("{LISTING_ID}",$rows['listingID'],$str);
			$str = str_replace("{LISTING_NAME}",$rows['listingName'],$str);
			$str = str_replace("{LISTING_CITY}",$objects->whatCity($rows['listingCity']),$str);
			$str = str_replace("{LISTING_STATE}",$objects->whatState($rows['listingState']),$str);
			$str = str_replace("{LISTING_ZIP}",$rows['listingZip'],$str);
			$str = str_replace("{LISTING_PRICE}",number_format($rows['listingPrice']),$str);
			$str = str_replace("{LISTING_BR}",$rows['bedrooms'],$str);
			
			$linkName = ereg_replace("[^A-Za-z0-9]", "-", $rows['listingName']);
			$str = str_replace("{photo1}","<a href='details_".$rows['ID']."/".$linkName.".html'>".$photos->singlePhoto($rows['ID'],100,'')."</a>",$str);			
			$results .= $str;						
			}
			
			$nextPrev = file_get_contents(SCRIPT_PATH."/templates/nextprev.php");
			
			foreach($_POST as $key=>$value)
				{
				$hidden .= "<input name='".$key."' type='hidden' value='".$value."'>";
				}
			$nextPrev = str_replace("{hidden}",$hidden,$nextPrev);
			
			$keywords = $objects->whatCity($_POST['city']);
			$keywords .= ", ".$objects->whatType($_POST['propertyType']);
			$keywords .= ", ".$objects->whatCategory($_POST['categoryCode']);
			$pageData = $this->createSearchPage($results,$nextPrev,$keywords);
									
			if(mysql_num_rows($searchQuery) >= $limit)
				{
				$pageData = str_replace("{PAGEFWD}",($_GET['page'] + 1),$pageData);
				}else
					{
					$pageData = str_replace("{PAGEFWD}","1",$pageData);
					}
					
			if($_GET['page']  > 1)
				{
				$pageData = str_replace("{PAGEBACK}",($_GET['page'] - 1),$pageData);
				}else
					{
					$pageData = str_replace("{PAGEBACK}","1",$pageData);
					}
					
			$pageData = str_replace("{results_found}",mysql_num_rows($sqlPage),$pageData);
						
			return $pageData;	
						
		}else //do a 'keyword' search
			{
			$limit = $listAmount;
			if(!isset($_GET['page']))
				{
				$page = 1;
				}else
					{
					$page = $_GET['page'];
					}
			$offset = ($page -1) * $limit;
			
			$objects = new Create;
			$photos = new Photos;
			$sql = 	"SELECT ListingsMain.*,ListingDimData.ID,ListingDimData.bedrooms FROM ListingsMain 
			LEFT JOIN ListingDimData ON ListingsMain.ID=ListingDimData.ID WHERE  ListingsMain.listingStatus = 'Current' and
				( listingID LIKE '%".$keyword."%' ) OR 
				( listingName LIKE '%".$keyword."%' ) OR  
				( listingCity LIKE '%".$keyword."%' ) OR 
				( listingState LIKE '%".$keyword."%' ) OR 
				( listingZip LIKE '%".$keyword."%' ) LIMIT ".$offset.",".$limit;
				
			$sqlPage = 	mysql_query("SELECT ListingsMain.*,ListingDimData.ID,ListingDimData.bedrooms FROM ListingsMain 
			LEFT JOIN ListingDimData ON ListingsMain.ID=ListingDimData.ID WHERE  ListingsMain.listingStatus = 'Current' and
				( listingID LIKE '%".$keyword."%' ) OR 
				( listingName LIKE '%".$keyword."%' ) OR  
				( listingCity LIKE '%".$keyword."%' ) OR 
				( listingState LIKE '%".$keyword."%' ) OR 
				( listingZip LIKE '%".$keyword."%' )") or die( mysql_error() );
			//echo $sql;		
			$query = mysql_query($sql) or die( mysql_error() );
 
			while($rows = mysql_fetch_assoc($query))
				{
				$linkName = ereg_replace("[^A-Za-z0-9]", "-", $rows['listingName']);
				$str = file_get_contents(SCRIPT_PATH."/templates/listingbox.php"); //Grab the template file		
				$str = str_replace("{LISTING_ID}",$rows['listingID'],$str);
				$str = str_replace("{LISTING_NAME}","<a href='".SCRIPT_URL."/details_".$rows['ID']."/".$linkName.".html'>".$rows['listingName']."</a>",$str);
				$str = str_replace("{LISTING_CITY}",$objects->whatCity($rows['listingCity']),$str);
				$str = str_replace("{LISTING_STATE}",$objects->whatState($rows['listingState']),$str);
				$str = str_replace("{LISTING_ZIP}",$rows['listingZip'],$str);
				$str = str_replace("{LISTING_PRICE}",number_format($rows['listingPrice']),$str);
				$str = str_replace("{LISTING_BR}",$rows['bedrooms'],$str);
				$str = str_replace("{photo1}","<a href='details_".$rows['ID']."/".$linkName.".html'>".$photos->singlePhoto($rows['ID'],100,'')."</a>",$str);			
				$results .= $str;					
				}
			
			$nextPrev = file_get_contents(SCRIPT_PATH."/templates/nextprev.php");
			
			foreach($_POST as $key=>$value)
				{
				$hidden .= "<input name='".$key."' type='hidden' value='".$value."'>";
				}
			$nextPrev = str_replace("{hidden}",$hidden,$nextPrev);
			
			$pageData = $this->createSearchPage($results,$nextPrev,$keyword);
			
			if(mysql_num_rows($query) >= $limit)
				{
				$pageData = str_replace("{PAGEFWD}",($_GET['page'] + 1),$pageData);
				}else
					{
					$pageData = str_replace("{PAGEFWD}","1",$pageData);
					}	
					
			if($_GET['page']  > 1)
				{
				$pageData = str_replace("{PAGEBACK}",($_GET['page'] - 1),$pageData);
				}else
					{
					$pageData = str_replace("{PAGEBACK}","1",$pageData);
					}
			$pageData = str_replace("{results_found}",mysql_num_rows($sqlPage),$pageData);
			
			return $pageData;	
			}
	//echo "<pre>".print_r($searchQuery)."</pre>"; exit;
	}///end searchListings
[+][-]05.04.2008 at 06:52AM PDT, ID: 21495661

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.04.2008 at 07:49PM PDT, ID: 21497876

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.05.2008 at 05:01AM PDT, ID: 21499301

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.06.2008 at 08:24PM PDT, ID: 21512987

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.07.2008 at 03:39AM PDT, ID: 21514921

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP Scripting Language, MySQL Server
Tags: PHP
Sign Up Now!
Solution Provided By: VoteyDisciple
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628