Jump to content

Search With Multiple Filters


telepunk

Recommended Posts

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
Share on other sites

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
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.