【问题标题】:I have an issue with my loops to show the result value我的循环显示结果值有问题
【发布时间】:2013-10-15 16:40:56
【问题描述】:
 $array_of_time = array ();
 $start_time    = strtotime ("$app_date 07:00");
 $end_time      = strtotime ("$app_date 22:00");
 $mins  = 04 * 60;

while ($start_time <= $end_time)
{
   $array_of_time[] = date ('h:i a', $start_time);
   $start_time += $mins;
}

echo "<div style='width:700px' class='time_slot_div'>";
echo "<p class='time_slot_p'> Time Slot :</p>";

foreach($array_of_time as $key=>$value)
{

    for($i = 0; $i < count($app_data); $i++)
    {
        $book_time=$app_data[$i]["appointment_time"];
        if($value==$book_time)
        {
           echo "<a  class='time_slot_a_book'>$value</a>&nbsp;";    
        } else {
           echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a>&nbsp;";
        }
    }
}

echo "</div>";

这里的 foreach 循环可以和 for 循环一样多次运行,但我想显示与 foreach 值和 for 循环值不匹配的链接。

foreach 循环值(如上午 7:20)不是来自数据库,但 for 循环值(如上午 7:20)来自数据库,因此如果上午 7:20==7:20 则它运行的 if 语句工作正常,但问题是如果我在 for 循环中得到 2 个值,它会运行 2 次。它应该只运行我的 div 一次。

【问题讨论】:

    标签: php mysql for-loop foreach


    【解决方案1】:

    如果参观了您的网站,我会修改副本:

    $array_of_time = array ();
    $start_time    = strtotime ("$app_date 07:00");
    $end_time      = strtotime ("$app_date 22:00");
    $mins  = 04 * 60;
    
    while ($start_time <= $end_time)
    {
       $array_of_time[] = date ('h:i a', $start_time);
       $start_time += $mins;
    }
    
    echo "<div style='width:700px' class='time_slot_div'>";
    echo "<p class='time_slot_p'> Time Slot :</p>";
    
    foreach($array_of_time as $key=>$value)
    {
      //
      // get the appointing state:
      //
      $bAppointed = false;
      for($i = 0; $i < count($app_data); $i++)
      {
        $book_time = $app_data[$i]["appointment_time"];
        $bAppointed = $value == $book_time;
        if($bAppointed)
        {
          break;
        }
      }
      //
      // print the time slot:
      //
      if($bAppointed)
      {
         echo "<a  class='time_slot_a_book'>$value</a>&nbsp;";    
      } else {
         echo "<a href='#' onclick='get_time_value(\"$value\");' class='time_slot_a'>$value</a>&nbsp;";
      }
    }
    
    echo "</div>";
    

    ?

    好点了吗?

    【讨论】:

    • 你能解释一下你想要什么吗?你也想打破 foreach($array_of_time as $key=>$value) 吗?尽快以身作则。
    • foreach 循环显示时间从上午 7:00 到晚上 10:00,时隙间隔为 4 分钟,例如。上午 7:00 上午 7:04 上午 7:08 依此类推.... for 循环的第二个循环正在获取已保存时间段的数据库的值,假设数据库中已保存值 7:04 am 7 :08 am 在两个单独的行中然后它将通过使用查询获取值 after for 循环将检查在我们的情况下保存了多少个时间段,即 7:04 am 7:08 am 然后它将与 foreach 匹配循环值如果匹配,它将进入 if 语句,否则在 else 语句中
    • 最好在问题中解释更多。给出一个示例时间列表以及您想要显示为时间的确切时间...
    • please go here webmakerindia.com/doctor/add_appointment.php login as username:ajit password:123 然后登录后去预约并选择/点击添加预约 表格出来后请选择医生姓名和日期然后数据就会出来保存按钮下方的时间正在重复。我不想重复时间
    • thanx jacouh 我得到了解决方案:D
    猜你喜欢
    • 2013-10-22
    • 2022-07-06
    • 2012-04-08
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 2014-12-23
    相关资源
    最近更新 更多