0

Hi I've set up a website which gathers results from a mysql database which allows the user to sort the results according to some preferences in 2 separate select lists.

It works as long as you make a selection from both boxes before submitting.

I would like for results to be shown if the user makes a selection from only one of the lists.

This is the code I am using right now; I can see why it only displays results if all menus have selected options but haven't a clue how to alter it to do as I wish.

$bedrooms=$_POST['bedrooms'];
$bathrooms=$_POST['bathrooms'];

$query=
"SELECT *
FROM inventory
WHERE bedrooms like '$bedrooms' AND bathrooms like '$bathrooms'
ORDER BY model_name ASC";

Can anyone help me out?

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by oliv1
0
$condition="";

if(isset($_POST['bedrooms']) && isset($_POST['bathrooms']) )
    $condition=" where bedrooms like '{$_POST['bedrooms']}' AND bathrooms like '{$_POST['bathrooms']}' "; 
elseif(!isset($_POST['bedrooms']) && isset($_POST['bathrooms']) )
    $condition=" where bathrooms like  '{$_POST['bathrooms']}' "; 
elseif(isset($_POST['bedrooms']) && !isset($_POST['bathrooms']) )
    $condition=" where bedrooms like '{$_POST['bedrooms']}' "; 


$query= "SELECT * FROM inventory {$condition} ORDER BY model_name ASC";

Edited by urtrivedi: n/a

0

Thanks urtrivedi,

I tried out your code, and it unfortunately didn't display any results when only one selection was made.

0

Hi,
try this code.

$bedrooms=$_POST['bedrooms'];
$bathrooms=$_POST['bathrooms'];  
	  
$whereStr = ' 1=1 ';
	  
if(trim($bedrooms)!='') $whereStr.= " AND  bedrooms like '$bedrooms'" ;
if(trim($bathrooms)!='') $whereStr.= " AND  bathrooms like '$bathrooms'" ;
	  
$query=   "SELECT * FROM inventory WHERE ".$whereStr." ORDER BY model_name ASC";

Edited by vibhaJ: alignment

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.