Forum Moderators: coopster
This is driving me nuts. I have a questionnaire/form of 10 questions. 9 of them are set up with radio buttons, and 1 is a multi-choice checkbox. On submit, I need the user to go to a results page where a recordset of the results based on the values chosen is displayed. I can pass and query the radio button values no problem, but I don't know where to start with the checkbox values. The checkboxes will hold string values and I need the WHERE statement to query all or any instances of the strings in a particular field.
My php knowledge is next to nothing and I've spent the last couple of days fiddling with arrays, trying to master loops, and it's like squashing an air bubble under wallpaper - nail it in one spot and something else always pops up.
If I've explained the scenario clearly enough, could someone just help me get going in the right direction? For all I know I've probably been tackling this completely the wrong way.
Many thanks.
1. Make sure all grouped checkbox input tags have the same name with [] appended to each name:
<input type="checkbox" name="MachineType[]" value="9">
2. In your process module use the implode command to create a comma delimited string of values:
$machineTypeList= implode(", ", $_GET['MachineType']);
3. In your where clause you could use the IN list command (assuming you are using MYSQL):
$whereClause = "h.machineTypeId IN (" . $machineTypeList . ")"
You'll notice this method only allows you to access string values that the user has explicitly checked. If you need to also access unchecked values you'll have to try a different approach.
I'll outline the closest scenario that I can below, and if anyone can still help I'd appreciate it.
The form passes an array, for example, temperment[]. It gets passed as a string value and would have checkbox values like:
grumpy
vicious
playful
stubborn
etc
In this example the table would be called 'Pets', and one of the fields would be 'description' that would contain a small paragraph of text.
I need the WHERE statement to look in the field 'description' in the table 'Pets' for all or any instances of the values checked in the form. It doesn't matter if none of the checkboxes are checked.
Does this sound easy to anyone?
Thanks.
the way I would tackle this, is count the amount of occurences of the checkbox. Ofcourse they would all have to have the same name...say option[]
Something like: $nr_options = count($_POST['options']); //(or $_GET, depending on what you use)
next query the DB with a loop
for ($i=0; $i<$nr_options; $i++){ //while $i < nr of times the box was checked..perform a query
$string= $_POST['options'][$i]; //this is the string you are looking for at the current position
$query= 'SELECT * FROM Pets'. " WHERE description LIKE '%".$string."%'".
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
//rest of the actions you wanne perform would probably go here too...in the loop...like echo statements etc
}
maybe there is a better way, I started with PHP 5 months ago....so suggestions are welcome...but I think you could use this
I suspect there are many ways to skin this cat, but unfortunately I always seem to trip up with the finish line in sight. Damn, this programming lark is frustrating.
Thanks to all for your help.