duaiwo9093 2014-01-24 17:54
浏览 97
已采纳

对于带有foreach循环的循环吗? PHP

I am trying to make the following happen.

Go through a for loop and produce a set of 7 select boxes with time options in them. I have this loop working fine right now. This is for a form with times for start of an event for each day of the week.

On user wanting to change the values, I would like to produce the same 7 boxes however foreach of the values entered in the database I would like to add the original selected value for that date, and provide the please select value for the of the boxes.

foreach loop for the times

foreach ($times as $time) {
    echo '<option value="' . $time['num'] . '"'; if ($event == $time['dec']){echo 'selected="selected"';};
            echo '>';
            echo $time['clock'];
            echo '</option>';
}

Times Array

$times = array( array('num' => 70000, 'dec' => '07:00:00', 'clock' => '7:00am'), array('num' => 71500, 'dec' => '07:15:00', 'clock' => '7:15am') etc…

This produces the select options.

Then the foreach to get the existing values.

foreach ($event -> Selected as $select) {
   $event = $select -> Start;
}

This is all working right now but only produces say 3 selects if 3 records exist, I would like to produce 7 selects with the first 3 selects with the DB value, if 3 records exist and 4 empty selects

  • 写回答

2条回答 默认 最新

  • duanchan5458 2014-01-24 18:00
    关注

    The array contains only a limited number of entries, so the loop will iterate only up to this number.

    You can work around this by checking how many elments are in the array, and outputting empty values for the rest.

    for example add the following after the loop:

    $count = 7 - count($times);
    for($i = 0; $i < $count; $i ++)
    {
        echo '<option></option>';
    }
    

    The above is a simple loop that outputs empty entries to ensure there are 7 drop down values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图