telepunk Posted November 13, 2012 Share Posted November 13, 2012 Not sure is this is a PHP or MYSQL question.. I have a website and am trying to make a search function. Example would be to search by minimum price, maximum price, minimum square feet, area of town, etc... All of the fields are optional also, so if they leave them blank the search needs to reflect that. I'm fluent in PHP but am running into a brickwall with this one, this is the direction I'm going in, can someone tell me what I'm doing wrong?? *pseudo code... if($search_minprice != ''){ $result = mysql_query("SELECT id FROM listing WHERE price >= '$search_minprice' ORDER BY id ASC "); $row = mysql_fetch_array($result); foreach ($row as &$value) { if($search_livsqft != ''){ $result2 = mysql_query("SELECT * FROM listing WHERE id = '$value' AND livsqft >= '$search_livsqft ORDER BY id ASC "); while($row2 = mysql_fetch_array($result2)){ $street= $row2['street']; echo $street."<br>"; }} }} I'm not too familar with foreach but it seems the way to go for this, I've always been able to avoid using foreach to my own detriment... Any help would be appreciated, thanks! Link to comment https://forums.phpfreaks.com/topic/270647-search-with-multiple-filters/ Share on other sites More sharing options...
Jessica Posted November 13, 2012 Share Posted November 13, 2012 Never ever ever do queries inside of loops! You simply need to have all of your search criteria in one query. Link to comment https://forums.phpfreaks.com/topic/270647-search-with-multiple-filters/#findComment-1392130 Share on other sites More sharing options...
telepunk Posted November 13, 2012 Author Share Posted November 13, 2012 never fails, as soon as I post, I find the answer... if(!empty($search_minprice)) { $wh = " price >= '{$search_minprice}' "; } if(!empty($search_maxprice)) { $wh .= " AND price <= '{$search_maxprice}' "; } $result = mysql_query("SELECT * FROM listing WHERE ".$wh); $listcount = mysql_num_rows($result); while($row = mysql_fetch_array($result)){ $listid = $row['id']; echo $listid."<br>"; } Link to comment https://forums.phpfreaks.com/topic/270647-search-with-multiple-filters/#findComment-1392137 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.