Re: Checkbox Array Question - Iteration
| From: | Brent Baisley | Date: | Tue, 17 Aug 2004 20:17:01 +0000 |
| Subject: | Re: Checkbox Array Question - Iteration | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-35754@lists.php.net to get a copy of this message | ||
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: <snip> while($row = mysql_fetch_array($curRecord)){ $vid=$row["vid"]; $pID=$row["pID"]; $kitchen = unserialize($row["kitchen"]); $kitchenevt = unserialize($row["kitchenevt"]); $profesional = unserialize($row["professional"]); . . . } //BUNCHA' IRRELEVENT STUFF CUT OUT OF HERE <span class="bodyBold">General Kitchen Help</span><br> <?php echo "<input type=\"checkbox\" name=\"kitchen[waitstaff]\""; if(in_array("waitstaff",$kitchen)){ echo "checked"; } echo "> <span class=\"body\">Waiter/Waitress</span><br>"; echo "<input type=\"checkbox\" name=\"kitchen[cook]\""; if(in_array("cook",$kitchen)){ echo "checked"; } echo "> <span class=\"body\">Cook</span><br>"; . . . //AND SO ON FOR EVERY SINGLE CHECKBOX. ?> </snip> 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. Rich -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Brent Baisley Systems Architect Landover Associates, Inc. Search & Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577