dqrb4228 2013-04-02 04:28
浏览 10
已采纳

从数组填充复选框

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

  • 写回答

2条回答 默认 最新

  • dongtang1910 2013-04-02 04:41
    关注

    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 } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀