Jump to content


Photo

PHP Checkboxes, dynamically change MySQL query


  • Please log in to reply
4 replies to this topic

#1 DillyDong

DillyDong
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 August 2006 - 03:17 PM

Hi all! Let's say I have the following ten checkboxes:

<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" />

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:

"SELECT * FROM `table` WHERE (`list_number` = 1 OR `list_number` = 2)
..etc

How would I generate this MySQL query most efficiently with ten or more checkboxes? I was thinking of using
foreach()
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!


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 09 August 2006 - 03:34 PM

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:
<?php

if(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">
  <?php
for($i = 1; $i <= 10; $i++)
{
    echo "<input name=\"checkbox[]\" type=\"checkbox\" value=\"{$i}\" />&nbsp; &nbsp;Checkbox{$i}<br />\n  ";
}
?>
  <input type="submit" name="submit" value="Generate Query" />
</form>


#3 DillyDong

DillyDong
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 09 August 2006 - 05:29 PM

Dear wildteen88,

Thanks a lot for the quick reply! That help was invaluable.

Sincerely,
DillyDong

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 09 August 2006 - 08:50 PM

Slightly easier is to put the checked values into a comma-delimited list

$list = join (',', $_POST['checkbox']);    #--> 1,3,4,7,8 for example

Then the query becomes

SELECT * FROM tablename WHERE list_number IN ($list)

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 DillyDong

DillyDong
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 10 August 2006 - 03:22 AM

Thanks for the simplified code! I never even knew the keyword "IN" existed!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users