Ugh! That's looks like it could get ugly to maintain.

What I would do is store your tasks in a table so when you want to add a task, you just add a record. You would then select the tasks from the table and build the check boxes from the list you got from the table.

$checkBoxGrid = '<span class="bodyBold">General Kitchen Help</span><br />';
foreach($task as $taskName) {
$checkBoxGrid .= '<input type="checkbox" name="kitchen['.$taskName.']" value="'.$taskName.'">';

Now you have your lists of checkboxes for which the code never needs to change as you add or remove tasks. Also, the code is the same for new entries or modifying existing entries. You then want to "check" the appropriate boxes, which you can do with a simple search and replace based on the serialized array that contains which boxes were checked.

foreach($task as $taskName) {
$checkBoxGrid = str_replace('name="kitchen['.$taskName.']"', 'name="kitchen['.$taskName.']" CHECKED', $checkBoxGrid);

What you are doing is searching on the name="kitchen[task]" string, which should be unique and replacing with an appended " CHECKED". The key is not to echo anything out, but to build things up in the $checkBoxGrid variable so you can modify it and deliver it when necessary. So altogether, that's about a half a dozen lines of code that you never need to change when you add or remove check boxes. You would probably need a couple of more lines to handle task categories, which should also be pulled from a table.

On Aug 17, 2004, at 1:37 PM, Hutchins, Richard wrote:

I am developing a page where people check off items for which they would
like to volunteer. For now, this page has around 50 checkboxes on it, but it
could conceivably change frequently and grow much larger than the current 50
items once it goes live.

I have all of the checkboxes arranged into nice little arrays and named
things like name="kitchen[waitstaff]" and name="kitchen[cook]",
name="professional[paint]". I take the checked boxes, serialize them, and
store them in columns in MySQL that bear the same name as the array (e.g.,
kitchen, professional). All of this works fine and I'm very happy with it.

Now, when the person revisits the page to update his/her selections, I query
the database and unserialize the column data. I then search the resulting
array and if the name of the checkbox is found in the array, I echo
"checked" to select the checkbox. See below:

        while($row = mysql_fetch_array($curRecord)){
                $kitchen = unserialize($row["kitchen"]);
                $kitchenevt = unserialize($row["kitchenevt"]);
                $profesional = unserialize($row["professional"]);


        <span class="bodyBold">General Kitchen Help</span><br>
                echo "<input type=\"checkbox\" name=\"kitchen[waitstaff]\"";
                        echo "checked";
                echo ">&nbsp;<span

                echo "<input type=\"checkbox\" name=\"kitchen[cook]\"";
                        echo "checked";
                echo ">&nbsp;<span class=\"body\">Cook</span><br>";


So, having set up a sample section of checkboxes on my page using the logic
in the snippet above, I am wondering if there is a much more efficient way
to look at the arrays that get returned from the database, check to see if
the given checkbox is contained therein, then, if it is, echo "checked". The
solution snippet above works just fine, but whenever I find myself
contemplating doing the same thing over and over in code, that's obviously a
flag for investigating an iterative solution.

Right now, though, I'm struggling with trying to noodle out whether setting
up some sort of iterative function would be more work than simply doing what
I'm doing now. Obviously, iteration is always great for handling repetitive
work, but I'm just not having much luck coming up with a solution.

I am aware that this is probably slightly off-topic since it isn't a direct
php/db question, but I was hoping somebody might be kind enough to offer up
suggestions as to how I might construct such a solution anyway.

Thanks in advance for you time.


PHP Database Mailing List (
To unsubscribe, visit:

Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577

PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to