开发者

MySQL select question: how to do a SELECT with any optional dropdowns?

OK, I've done my best to find a thread like this but no luck so far and need some help crafting my SQL query. I'm working on a real estate project and have a form with multiple dropdowns, like so:

   <select name="bed" id="bed">
     <option value="0">Any</option>    
        <option value="1">1 Bedroom</option&g开发者_运维知识库t;    
     <option value="2">2 Bedroom</option>    
     <option value="3">3 Bedroom</option>    
     <option value="4">4 Bedroom</option>    
     <option value="5">5 Bedroom</option>    
     <option value="6">6 Bedroom</option>    
    </select>

    <select name="bed" id="bed">
     <option value="0">Any</option>    
        <option value="1">1 Bedroom</option>    
     <option value="2">2 Bedroom</option>    
     <option value="3">3 Bedroom</option>    
     <option value="4">4 Bedroom</option>    
     <option value="5">5 Bedroom</option>    
     <option value="6">6 Bedroom</option>    
    </select>

If the user wants to search properties and selects both a 'bed' and a 'bath' number -- as in, 4 bed / 2 bath -- then it's easy:

SELECT * FROM exp_channel_data WHERE field_id_4 = '<?= $search['bed'] ?>' AND field_id_5 = '<?= $search['bath'] ?>

(In which I've captured the POST data in an array.) This works as expected.

But if the user picks only beds -- as in, show me all the properties that have 3 bedrooms however many baths -- and leaves the bath dropdown empty (or 'any'), then I only need a SELECT statement like the above but without the 'AND' in the 'WHERE'.

So my question is, how might I check for the existence of a non-zero amount for the bath dropdown and update the SELECT statement based on the POST array? I suspect it's much easier than I think, I've just been staring too long... Thanks in advance for even pointing me in the right direction.


I would do something like:

$query = "SELECT * FROM exp_channel_data WHERE 1 = 1 ";
if($search['bed']) $query .= "AND field_id_4 = ".$search['bed'].' ';
if($search['bath']) $query .= "AND field_id_5 = ".$search['bath'].' ';
$results = mysql_query($query);


<?php
$conditions = array();
if ($search['bed'] != 0)
    $conditions[] = '`field_id_4` = '.$search['bed'];

if ($search['bath'] != 0)
    $conditions[] = '`field_id_5` = '.$search['bath'];
$sql = "SELECT * FROM exp_channel_data".(!empty($conditions) ? " WHERE ".implode(' AND ', $conditions) : '');
?>


Here's something you can use and potentially expand in the future. I'm assuming you don't have anything else in that $search[] array - if you do, then you'd want a new array with just the applicable fields in it.

$query = "SELECT * FROM exp_channel_data"
$conditions = array();
$where = '';

foreach($search as $field => $value)
{
    if($value != 0)
    {
        $conditions[] = "$field = $value"
    }
}

$where = implode(' AND ', $conditions);
if($where != '')
{
    $where = " WHERE $where";
}

$query .= $where;
//now go run your query.

The upside here is that if you have several more items, this works for them too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜