Forum Moderators: coopster

Message Too Old, No Replies

Multiple checkbox => MySql query, query...

Need help querying multiple values

         

2oddSox

8:42 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



Hi there,

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.

apparelman2

9:15 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



OddSox,

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.

2oddSox

9:49 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



That's a pretty useful first post, apparelman2. Thanks, and welcome to WW.

I've got your first couple of points covered no problem, but I'm a bit of a stranger to the 'IN' command so I'll have to do a bit of research there.

Again, many thanks.

jusdrum

10:06 pm on Jan 20, 2005 (gmt 0)

10+ Year Member



I was actually just having to do a bit of work like this with checkboxes. I tried the IN command, but I found that if I wanted to look in a field in the same row, it wouldn't work. I used FIND_IN_SET and it works great.

select FieldName from table where FIND_IN_SET($Value,FieldName)

2oddSox

6:44 pm on Jan 21, 2005 (gmt 0)

10+ Year Member



Thanks for the assistance guys, but I'm still scratching my head over this one. Your suggestions lead me to this thread [webmasterworld.com] where jatar_k's helpful summary at the end had me hoping I'd cracked it, but I just get 'foreach' error messages all over the place.

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.

dmmh

9:15 pm on Jan 21, 2005 (gmt 0)

10+ Year Member



I hope I am getting what you mean and that I make sense :)

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

2oddSox

11:17 pm on Jan 21, 2005 (gmt 0)

10+ Year Member



Thanks dmmh. I tried your code but forgot to change the field name so I got an error message which spat out the actual first element of the array. Which, I suppose, is a good thing, as it meant the rest of the code worked and the array was passing thru. I then changed the field name to its proper value and subsequently got a blank screen which means I have no idea where to start looking to see what's wrong.

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.

dmmh

6:08 am on Jan 22, 2005 (gmt 0)

10+ Year Member



past us the complete code :)
dont forget to strip passwords etc