Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a bunch of checkboxes when submitted the values (if checked) are added to an array and serialized then saved on my db in one row.

Now I'd like to show the checkboxes on an edit page with the appropriate checkboxes checked.

I'm currently outputting the checkboxes as such...

HTML

6 w <input type="checkbox" name="size[]" value="6 w">
7 w <input type="checkbox" name="size[]" value="7 w">
8 w <input type="checkbox" name="size[]" value="8 w">

I've tried echoing out a checked value but not sure how to get the right ones checked. This was one suggestion from a site I came across...

HTML

6 w <input type="checkbox" name="size[]" value="6 w" <? echo $checked['6 w'] ?> >
7 w <input type="checkbox" name="size[]" value="7 w" <? echo $checked['7 w'] ?> >
8 w <input type="checkbox" name="size[]" value="8 w" <? echo $checked['8 w'] ?> >

PHP

$size = unserialize($row["size"]);        
$array = "";
foreach($size as $size_available)
{
  $array .= "$size_available,";
}
  $array_explode = explode(',',$array);

    //$checked = "";
    foreach ($array_explode as $v){
    $checked[$v] = "checked='CHECKED'";
}

This almost works showing the correct checkboxes checked but any checkboxes not stored in the array give out an undefined index error. I'm guessing because it is outputting an empty variable???

Any suggestions where to go from here or how to do this a better way?

Cheers

share|improve this question
    
Probably you are getting this error when your $array_explode variable is empty, so before doing foreach you should verify if the array_explode var is empty: if(count($array_explode) > 0) foreach... Use the same method for $size array – Matei Mihai Sep 24 '12 at 4:30
    
@MateiMihai You're absolutely right, thanks for the fix. – Bjorn Sep 24 '12 at 4:48
up vote 1 down vote accepted

Validate that the index is set before accessing it with isset() -

<? echo (isset($checked['8 w']))? $checked['8 w'] : "" ?>
share|improve this answer
    
This worked perfectly thanks – Bjorn Sep 24 '12 at 4:36
    
As other answers pointed out, you should also double check $array_explode = explode(',',$array); - if $array is empty, the call to explode() will return false, and your foreach() will fail. – doublesharp Sep 24 '12 at 4:38
    
Yeah did get that and fixed ;) – Bjorn Sep 24 '12 at 4:48

Change this:

foreach($size as $size_available)

for this:

foreach($_REQUEST['size'] as $size_available)

There's no need for the unserialize command.

share|improve this answer
    
The issue is with the "undefined index error" when printing the values in the HTML due to some of the keys not being set in the array if they aren't saved in the database. Although the code may not be optimal, the issue is that there is not check for isset() on the array. – doublesharp Sep 24 '12 at 4:30
    
@doublesharp Partially agree. It may raise an "undefined index" warning (not error) if ALL the checkboxes are empty. If there is at least 1 checkbox checked it will work. And isset validation will correct this. – Hernan Velasquez Sep 24 '12 at 4:34
    
I don't think that is where the "undefined index" would originate... it's with the $checked array. I am assuming that it is defined as $checked = array() somewhere not in the example, which may not be a valid assumption so I will update my answer accordingly. – doublesharp Sep 24 '12 at 4:36
    
This didn't seem to work for what ever reason so at the moment I've just unserialized it – Bjorn Sep 24 '12 at 4:37
    
Only inclusion of $checked is with $checked[$v] = "checked='CHECKED'"; – Bjorn Sep 24 '12 at 4:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.