0
$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 一次。

4

1 回答 1

0

使用数组来存储已显示的项目。

<?php

foreach($array_of_time as $key=>$value)
{
    $tag_list = array();
    for($i = 0; $i < count($app_data); $i++)
    {
        $book_time=$app_data[$i]["appointment_time"];

        // shown, skip
        if (isset($tag_list[$book_time]))
        {
            continue;
        }

        // mark as show
        $tag_list[$book_time] = 1;

        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;";
        }
    }
}
于 2013-10-14T12:53:01.833 回答