Forums

Setting Checkbox Array items to 'Checked'

meckermann 24 Aug, 2008
Hi there,

I've create a Chronoforms registration page that uses a checkbox array and I use implode to add the selected values to a database field. This works great.

I've also created a profile page to allow the user to view or change the data, but I can't work out how to set the checkboxes to checked using the data in the database field.

Can anyone help?

Ecker
GreyHead 24 Aug, 2008
Hi meckermann,

That's one of those 'easier to do than to describe' tasks.

Basically, create an empty array using the checkbox names as keys; explode the saved data back into this array so that you have names + values; then use a PHP loop to write the checkbox array setting the non-blank values to show checked="checked".
<?php
$check_array = array('box_1' => '', 'box_2 => '', 'box_3' => '');
$value_array = explode('some string from the database);
$check_array = array_combine($check_array, $value_array);
foreach ( $check_array as $key => $value ) {
  if ( $value ) {
    $checked = "checked='checked'";
  } else { 
    $checked = "";
  }
  echo "<input type='checkbox' name='$key' value='$value' $checked />";
}
Not tested so may need debugging.
This code is a bit basic, you'd need to modify it to add field names, and maybe $_POST values.

Bob
meckermann 25 Aug, 2008
Thanks for that Bob,

Would you be able to explain a couple of things...

$check_array - the checkbox names are the same (i.e. an array called cbname[]) so do I assign the keys as cbname[0], cbname[1], etc?

$value_array - is the string from the database just a select statement on the field with the checkbox values? Would you mind giving me a quick example?

Thanks again!

Ecker
GreyHead 25 Aug, 2008
Hi meckermann,

You can use the $cbname array OK as it is. The only purpose of this array is to make sure that all the checkboxes are created with the correct names. So maybe:
$cbname = array_fill(0, 9, ""); // creates an array from cbname[0] to cbname[9]


For the explode you just need to reverse the implode statement that was used to save the values in the database.

For example, if you have
$cbname = array( '0' => 'OK', '1' => 'OK', '2' => '', '3' => 'OK', '4' => '');
$db_string = implode(', ', $cbname);
then $db_string will be something like "OK, OK, , OK, " and you'll need to explode this back to the array:
$cbname_values = explode(', ', $db_string);


Bob
meckermann 26 Aug, 2008
Thanks very much Bob,

Works a treat now.

I actually used nested foreach statements to check the arrays because unchecked values aren't entered into the database.

Here's my code if you or anyone are interested:
<?php
global $my;
$sql = "SELECT cb_volareyou FROM jos_comprofiler WHERE id = $my->id";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    $value_array = explode(',', $row['cb_volareyou']);
}
$check_array = array('Student','Home Based','Employed','Unemployed','Centrelink Referred','Retired','Corporate Volunteer Group','Other (please specify)');
foreach ( $check_array as $value1 ) {
  foreach ($value_array as $value2 ) {
    if ( $value1 == $value2) {
      $checked = "checked='checked'";
        break 1;
    } else { 
      $checked = "";
    }
  }
 echo "<input type='checkbox' name='cb_whoareyou[]' value='$value1' $checked />$value1<br>";
}
Dionysus 14 Sep, 2008
Where do you add this code too??
Max_admin 14 Sep, 2008
in the HTML code where the checkboxes will be available I think.
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 14 Sep, 2008
Hi Dionysus,

Yes, as Max says, it goes in the Form HTML.

Bob
This topic is locked and no more replies can be posted.