Jump to content

multiple selects


lional

Recommended Posts

Hi All

Got a quick question

I am writing a series of options that will end up pulling the search from a mysql table.

I have three options provinces, city, suburb where there is an all option for each of these relating to the previous selection.

I could however end up where province could either be all or a provinces, city could ne all or a city and suburb could be all or a city.

If I want to run a query based on these conditions what would be the easiest conditional.

there could be 9 if statements for the following:

province                                    city                                        suburb

all                                            all                                          all

all                                            all                                          suburb

all                                            city                                        all

do I need to do 9 conditions or is there a shorter form to do the query

 

I hope the above is clear

 

Thanks

 

Lional

Link to comment
https://forums.phpfreaks.com/topic/141701-multiple-selects/
Share on other sites

You would just build the query based on the choices

 

"SELECT * FROM table WHERE province = '%".mysql_real_escape_string($_POST['province'])."%' AND city = '%".mysql_real_escape_string($_POST['city'])."%'" AND suburb = '%".mysql_real_escape_string($_POST['suburb'])."%'";

Link to comment
https://forums.phpfreaks.com/topic/141701-multiple-selects/#findComment-741855
Share on other sites

if($_POST['province']!='all')
{
    $whereList[] = "province = '".mysql_real_escape_string($_POST['province'])."'";
}
if($_POST['city']!='all')
{
    $whereList[] = "city = '".mysql_real_escape_string($_POST['city'])."'";
}
if($_POST['suburb']!='all')
{
    $whereList[] = "suburb = '".mysql_real_escape_string($_POST['suburb'])."'";
}

$whereClause = (count($whereList)) ? ' WHERE ' . implode(' OR ', $whereList) : '';

$query = "SELECT * FROM table $whereClause";

Link to comment
https://forums.phpfreaks.com/topic/141701-multiple-selects/#findComment-741883
Share on other sites

Maybe I am misunderstanding, but if you selected all, you would just pass a blank value... The resulting would be ...AND field = '%%' which would match anything.

 

so If I chose 1 province, all cities, and all suburbs, a query might look like

 

"SELECT * FROM table WHERE province = '%selectedprovince%' AND city = '%%' AND suburb = '%%'";

 

mjdamato's code will do the same thing, without the use of wildcards... definately the better way to go.

 

 

Link to comment
https://forums.phpfreaks.com/topic/141701-multiple-selects/#findComment-742681
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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