0

Why does this code only check the first checkbox?

$activetheme = $theme->selectActiveThemeName();
foreach($blocks->availableBlockAreas($activetheme) as $block_name => $block_area_number) {
    $selected = (in_array($block_area_number, $block_areas)) ? ' checked="checked"' : '';
    echo '<p><input class="block_checkboxes" '. $selected .' type="checkbox" name="block_areas[]" value="'.$block_area_number.'" />'.ucfirst($block_name).'</p>';
}

$block_areas is an array:

Array ( [0] => 1,2,3 )

$blocks->availableBlockAreas($activetheme) returns

Array ( [Homepage - Left Column] => 1 [Homepage - Right Column] => 2 [Custom Page - Left Column] => 3 [Custom Page - Right Column] => 4 )

The $block_area_number should iterate?

EDIT:

After Geoandri' answer i came up with this fix, first i serialized the array when inserting into the DB then:

$block_areas[] = unserialize($row['block_areas']);

$selected = (in_array($block_area_number, $view->array_flatten($block_areas))) ? ' checked="checked"' : '';

1 Answer 1

1

Try changing $block_areas to

$block_areas = array(1,2,3);

This way I think will work.

Sign up to request clarification or add additional context in comments.

2 Comments

$block_areas comes from a database as 1,2,3 but can be 1,2,3,4 1,2,3,4,5,6 etc how can i change the array i have into the 1,2,3 format?
You gave me a great idea with what you said, ill add it to my question, thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.