Forum Moderators: coopster

Message Too Old, No Replies

Muti Form Post Handling w/ empty Variables

         

rogart

7:23 pm on May 23, 2008 (gmt 0)

10+ Year Member



In short, I have a form with three drop down boxes.
I want to let the user select any combination.
Example:
1 - City with 10 cities
2 - Time four times of day
3 - Place 25 different names

Each of the 25 places may or may not exist in each city.
I want the user to be able to select:
City
Time
Place
City & Place
City & Time & Place
etc...
Also I have only one submit button for the form using method POST.

I have done it with multiple nested if..then and if..elseif checking each condition, but both are slow and seem incredibly inefficient.

rob7591

10:48 pm on May 24, 2008 (gmt 0)

10+ Year Member



I'm not sure if I fully understand what you are doing, but I think you're trying to find a fast way to check if each place exists in the city.

You can do it with an array like this:
$cities = array();
$cities['City1'] = 'Place1,Place2,Place5';
$cities['City2'] = 'Place2,Place4,Place6';
etc.

Where each element with the key of each city has a comma delimited list of all the places that exist inside of them.

Then on the form submit you can do:
if (stristr($cities[$_POST['city']], $_POST['place'])) {
/*The place exists in the city */

I hope that explains it.

rogart

2:48 am on May 25, 2008 (gmt 0)

10+ Year Member



Thanks Rob,

Sorry for being vague. The user has three options, look for a place, see what places are in a city, or see what places in which cities are open at certain times of the day.
The output for each database entry is:
Name Place, Times Open
Address, City
Map Link

I think what I need is a clever select statement like:

Select * from table where city=$_POST["city"] and city!=$_POST["city"] and/or place=$_POST["place"] and place!=$_POST["place"] and/or time=$_POST["time"] and time!=$_POST["time"]

An example: They select noon-4pm

they get back all the places open noon-4pm in the database.

Then they select a Sam's Grocery and noon-4pm

they get back all the Sam's Grocery's open between noon-4pm.

Or they select Sam's Grocery

they get back all the times they are open and the cities they are in.

wheelie34

8:02 am on May 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rogart, here's the method I would use

accept all POST variables that could be sent then create different SELECT statements depending on what was passed IE (code below not properly formatted its just an example)

if isset $city && !$place {
$query = SELECT * FROM table WHERE city ='$city'
}
elseif isset $city && $place {
$query = SELECT * FROM table WHERE city ='$city' AND place ='$place'
}
elseif etc etc until you cover all possabilities

Then run your output using whichever $query is selected from the above

HTH

rob7591

2:03 pm on May 25, 2008 (gmt 0)

10+ Year Member



Yeah, or you can do
$where = '';
if ($city) $where .= ($where != '' ? " AND city = '$city'" : "city = '$city'");

Do that for each but replace the city with place, and time.
Then:

if ($where == '') $where = 1;

Then you'll have your $where section of your query.

then do:

$query = "SELECT * FROM table WHERE $where";