0

Hi I've data for year store as serialize like this

a:2:{i:0;s:4:"2011";i:1;s:4:"2013";}
and have a list of predefined year like this:
$current_year = date('Y');
for($year = 2011; $year < $current_year; $year++)
{
$year;
}

So, I want to populate a list of checkboxes like below:
[x] 2011
[ ] 2012
[x] 2013
...

If the year is not in the predefined year (in this case 2011,2012 and 2013), the year should be unchecked.

I've search but so far the nearest solution is not in PHP

1
  • echo "<input type='checkbox' value='$year'".(in_array($year,$Array)?" checked":"")." />"; like this? Commented Apr 2, 2013 at 4:32

2 Answers 2

3
$years = unserialize($mysql_years);
$current_year = date('Y');
for ($year = 2011; $year < $current_year; $year++) {
    $checked = '';
    if (in_array($year, $years)) {
        $checked = ' checked';
    }
    echo "<input type=checkbox value=$year$checked>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

I have found the solution. Check this.

<?php 
 $dat='a:2:{i:0;s:4:"2011";i:1;s:4:"2013";}';
 $data=unserialize($dat); 
 $current_year = date('Y');
for($year = 2011; $year <= $current_year; $year++) 
{
    if(in_array($year,$data))   {       $checked="CHECKED";     }   else { $checked=""; } ?>
    <input type="checkbox" value="<?php echo $year; ?>" name="year[]"  <?php echo $checked; ?> > <?php echo $year; ?> <br/>
<?php } ?>

2 Comments

BTW, your solution was exactly what i am looking for.
Great Fadli. Have a nice day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.