php - 我的循环显示结果值时遇到问题

标签 php mysql for-loop foreach

 $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 循环值不匹配的链接。

像 7:20 am 这样的 foreach 循环值不是来自数据库,但是像 7:20 am 这样的 for 循环值来自数据库,所以如果 7:20 am==7:20 am 那么它运行的 if 语句工作正常,但问题是,如果我在 for 循环中得到 2 个值,它就会运行 2 次。它应该只运行我的 div 一次。

最佳答案

如果浏览了您的网站,我会修改副本:

$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>";

这样更好吗?

关于php - 我的循环显示结果值时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19386510/

相关文章:

php - 如何在 PHP 中编写一个 foreach 循环来计算具有不同元素的 XML 元素的值?

java - java for循环的效率

mysql - 使用 Join 删除孤立记录

php - 使用日期格式搜索一段时间内的数据

python - 使用元素的幂创建对称数组

javascript - 循环访问对象名称时出现问题

PHP MySQL PDO 执行函数在两次使用相同参数时不给出任何结果

javascript - 将 Jquery 可过滤组合转换为选项选择

php - 在 WordPress 中的 foreach 循环内选择查询

php - MySQL CREATE 产生冗余索引