views:

66

answers:

2

Hi,

Following situation: I stored a checkbox array with implode in a mysql table field. Now, in order to update the checkboxes, I want to read the table field content, explode it into its parts and assign it to the respective checkboxes.

So far I managed to read out and explode the table field content into different chunks, my difficulty is how to assign to the respective checkboxes.

Here is the checkbox field:

<tr>
   <td><input class="field checkbox" type="checkbox" name="appbox[]" value="Automotive" <?php $appbox_checked ?> /><label class="choice">Automotive</label></td>
   <td><input class="field checkbox" type="checkbox" name="appbox[]" value="Backlights" <?php $appbox_checked ?> /><label class="choice">Backlights</label></td>
   <td><input class="field checkbox" type="checkbox" name="appbox[]" value="LED lighting" <?php $appbox_checked ?> /><label class="choice">LED lighting</label></td>
  </tr>
  <tr>
   <td><input class="field checkbox" type="checkbox" name="appbox[]" value="IR" <?php $appbox_checked ?> /><label class="choice">IR</label></td>
   <td><input class="field checkbox" type="checkbox" name="appbox[]" value="Signage/Traffic Lights" <?php $appbox_checked ?> /><label class="choice">Signage/Traffic lights</label></td>
   <td><input class="field checkbox" type="checkbox" name="appbox[]" value="Mobile Devices" <?php $appbox_checked ?> /><label class="choice">Mobile devices</label></td>
  </tr>

and here is the php code:

$storebox = explode(", ", $chunk0);
    for($i = 0; $i < count($storebox); $i++){
    echo "Piece $i = $storebox[$i] <br />";
    }

The chunk content matches the value field of the checkbox. So what I need is basically:

if 'chunk content' = 'checkbox value' then <?php $appbox_checked ?> will echo 'checked'

Or maybe there is a simplier solution. Thanks for you help guys!

A: 

All you have to do is add an attribute to your checkbox input elements called checked with a value of yes when your code has to render a "checked" checkbox:

<input type="checkbox" checked="yes" name="sports" value="soccer" />
Cypher
+1  A: 

Easiest way is to use in_array():

The place where you're writing <?php $appbox_checked ?>, replace it with this (example Automotive)

 <?php if(in_array('Automotive', $storebox)) echo 'checked="checked"'; ?>

Similarly, for all lines. e.g. for Backlights, replace it with:

 <?php if(in_array('Backlights', $storebox)) echo 'checked="checked"'; ?>
shamittomar
Thanks! That works for me.
Rolf