Page 1 of 1

php multiple checkboxes save in one field.

Posted: Tue Jun 07, 2011 7:26 pm
by cjkeane
hi everyone.

i have several check boxes which i need to save as an array into one field in a database.
i can save each check box with a separate name and save them in separate fields in the database, but i'd really like them saved as an array and then i only have to check the array to see if one check box is checked.

i'd appreciate any helpful hints people could provide.

this is my check box code.

Code: Select all

<input type="checkbox" name="ACLRole[]"  value="User"<?php echo ($ACLRole == 'User' ? " CHECKED" : ""); ?>>User<br />
<input type="checkbox" name="ACLRole[]"  value="Manager"<?php echo ($ACLRole == 'Manager' ? " CHECKED" : ""); ?>>Manager<br />
<input type="checkbox" name="ACLRole[]"  value="Admin" <?php echo ($ACLRole == 'Admin' ? " CHECKED" : ""); ?>>Administrator <br />


Re: php multiple checkboxes save in one field.

Posted: Tue Jun 07, 2011 7:41 pm
by Jonah Bron
Why do you want it in an array? I don't understand the advantage of that over individual columns.

Re: php multiple checkboxes save in one field.

Posted: Tue Jun 07, 2011 7:48 pm
by cjkeane
I wanted to validate or rather check the array to see if it had data, instead of validating separate check boxes.

Re: php multiple checkboxes save in one field.

Posted: Tue Jun 07, 2011 10:00 pm
by flying_circus
Serialize your array and store it in the database as a string.

Re: php multiple checkboxes save in one field.

Posted: Wed Jun 08, 2011 3:22 pm
by Jonah Bron
I understand what you want to do, I just don't know why. The number of checkboxes is constant, right?