I have the following variable in a php request:
myfile.php?location=0&bedrooms=0
This is the code I am using to retrieve it (I have indicate the field types because it is the only differnce between how I am handling the requests):
$location = intval($_REQUEST['location']);
Field Type = int(11)
$bedrooms = intval($_REQUEST['bedrooms']);
Field Type = tinyint(3)
This is how I am handling variables:
if ($location == 0)
$ilocation = '2,3,4,5,6,7';
else
$ilocation = $location;
if ($bedrooms == 0)
$ibedrooms = '-1,1,2,3,4,5,6,7,8,9';
else
$ibedrooms = $bedrooms;
This is how I am using it in my query:
Where realty.published = '1' AND locid IN ($ilocation) AND bedrooms IN ($bedrooms)
When i have a request such as location=0&bedrooms=1, it works fine but is bedrooms=0 I get no results.
When I change the bedrooms statement to
AND bedrooms IN (-1,1,2,3,4,5,6,7,8,9)
it works.
Should I have something other than ' ' (single quotes) in the bedrooms if else statement?
Is there a better way to handle this?
AND bedrooms IN ($bedrooms)
change this to
AND bedrooms IN ($ibedrooms)
I know I already asnwered this in the comments,but maybe it's better if you accept my answer as an actual answer so people can see it's answered instead of coming to an unanswered question only seeing it's answered already.
The way I'd handle that is like this.
if (!isset($_GET['location']) || intval($_GET['location']) == 0){
$ilocation = '2,3,4,5,6,7';
}
else {
$ilocation = intval($_GET['location']);
}
if (!isset($_GET['bedroom']) || intval($_GET['bedroom']) == 0){
$ibedrooms = '-1,1,2,3,4,5,6,7,8,9';
}
else {
$ibedrooms = intval($bedrooms);
}
That way, if no value is set in the URL, it default to select everything.