DillyDong Posted August 9, 2006 Share Posted August 9, 2006 Hi all! Let's say I have the following ten checkboxes:[code]<input name="checkbox_1" type="checkbox" value="1" /> <input name="checkbox_2" type="checkbox" value="1" /> <input name="checkbox_3" type="checkbox" value="1" /> <input name="checkbox_4" type="checkbox" value="1" /> <input name="checkbox_5" type="checkbox" value="1" /> <input name="checkbox_6" type="checkbox" value="1" /> <input name="checkbox_7" type="checkbox" value="1" /> <input name="checkbox_8" type="checkbox" value="1" /> <input name="checkbox_9" type="checkbox" value="1" /> <input name="checkbox_10" type="checkbox" value="1" />[/code]Now, for each checkbox the user checks, I want to update a MySQL query that will get values from each list corresponding to that number. For example, if they check boxes 1 and 2, I want the MySQL query to look like:[code]"SELECT * FROM `table` WHERE (`list_number` = 1 OR `list_number` = 2)[/code]..etcHow would I generate this MySQL query most efficiently with ten or more checkboxes? I was thinking of using [code]foreach()[/code] but I'm not sure if that's going to work. If someone could point me in the right direction that would be great! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/17025-php-checkboxes-dynamically-change-mysql-query/ Share on other sites More sharing options...
wildteen88 Posted August 9, 2006 Share Posted August 9, 2006 You'll wwant to give each checkbox the same name but prepend the name square brackets like so: checkbox[] that way any tickboxes that are checked get send as an array, which will help to produce your dynamic query. ALso you'll wan to give the checkbox a different value, say for checkbox1 use the value 1, for next checkbox give it a vlaue of 2 etc.Heres a demo:[code=php:0]<?phpif(isset($_POST['submit'])){ $sql = 'SELECT * FROM `table` WHERE (`list_number` = '; $sql .= implode(' OR `list_number` = ', $_POST['checkbox']) . ")"; echo '<code>' . $sql . "</code><br /><br />\n";}?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <?phpfor($i = 1; $i <= 10; $i++){ echo "<input name=\"checkbox[]\" type=\"checkbox\" value=\"{$i}\" /> Checkbox{$i}<br />\n ";}?> <input type="submit" name="submit" value="Generate Query" /></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17025-php-checkboxes-dynamically-change-mysql-query/#findComment-71858 Share on other sites More sharing options...
DillyDong Posted August 9, 2006 Author Share Posted August 9, 2006 Dear wildteen88,Thanks a lot for the quick reply! That help was invaluable.Sincerely,DillyDong Quote Link to comment https://forums.phpfreaks.com/topic/17025-php-checkboxes-dynamically-change-mysql-query/#findComment-71925 Share on other sites More sharing options...
Barand Posted August 9, 2006 Share Posted August 9, 2006 Slightly easier is to put the checked values into a comma-delimited list[code]$list = join (',', $_POST['checkbox']); #--> 1,3,4,7,8 for example[/code]Then the query becomes[code]SELECT * FROM tablename WHERE list_number IN ($list)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17025-php-checkboxes-dynamically-change-mysql-query/#findComment-72030 Share on other sites More sharing options...
DillyDong Posted August 10, 2006 Author Share Posted August 10, 2006 Thanks for the simplified code! I never even knew the keyword "IN" existed! Quote Link to comment https://forums.phpfreaks.com/topic/17025-php-checkboxes-dynamically-change-mysql-query/#findComment-72254 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.