日历系统预订错误,PHP 循环出错

Calendar system booking error, PHP looping gone wrong

目前,我正在尝试使预订系统看起来像这样(在 HTML 中轻松完成,PHP)

应用脚本和函数后,我得到:

代码:

function displayCalendar(){
    global $database;
    global $smarty;
    $sID = $_GET['serverid'];
    $database->query('SELECT * FROM bookings WHERE sID = :server');
    $database->bind(':server',$sID);
    $getServer = $database->fetchAll();
    $week = $_GET['week'];

    $times = array();
    for ($h = 6; $h < 18; $h++){
        for ($m = 0; $m < 60 ; $m += 60){
            $time = sprintf('%02d:%02d', $h, $m);
            $times["'$time'"] = "$time";
        }
    }

    $days = array(
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
        'Sunday',
    );

    echo '<tr>';
    for ($i = 0; $i <= 6; $i++) {
        echo '<th>'.$days[$i].'</th>';
    }
    echo '</tr>';

    foreach ($times as $time)
    {
        echo '<tr>';
        for($i = 0; $i <= 6; $i++) {
            foreach($getServer as $test => $row){
                if($row['time'] == $time && $row['day'] == $days[$i] && $row['week'] == $week)
                {
                    echo '<td><button type="submit" class="btn btn-danger" aria-label="Left Align">'.$row['time'].'</button></td>';
                }
                else
                {
                    echo '<td><button type="submit" class="btn btn-success" aria-label="Left Align"><a href="?page=booking&serverid='.$sID.'&week='.$week.'&day='.$days[$i].'&time='.$time.'">'.$time.'</a></button></td>';
                }
            }
        }
        echo '</tr>';
    }
} 

我对您的代码做了一些更改,但它可以正常工作。

关键是循环的 hasData() 函数,如果找到数据,它会执行 return 来中断循环。

您的代码继续循环,每次迭代都会回显。

function getData(){
    global $database;
    $sID = $_GET['serverid'];
    $database->query('SELECT * FROM bookings WHERE sID = :server');
    $database->bind(':server',$sID);
    $getServer = $database->fetchAll();

    return $getServer;
}


function hasData($getServer, $time, $day, $week, $sID){
    foreach($getServer as $row){
        if($row['time'] == $time && $row['day'] == $day && $row['week'] == $week)
        {
            return '<td><button type="submit" class="btn btn-danger" aria-label="Left Align">'.$time.'</button></td>';
            break;
        }
    }
    return '<td><button type="submit" class="btn btn-success" aria-label="Left Align"><a href="?page=booking&serverid='.$sID.'&week='.$week.'&day='.$day.'&time='.$time.'">'.$time.'</a></button></td>';
}

function displayCalendar(){
    global $database, $smarty;
    $sID = $_GET['serverid'];
    $getServer = getData();

    $week = $_GET['week'];

    $times = array();
    for ($h = 6; $h < 18; $h++){
        for ($m = 0; $m < 60 ; $m += 60){
            $time = sprintf('%02d:%02d', $h, $m);
            $times["'$time'"] = "$time";
        }
    }

    $days = array(
        'Monday',
        'Tuesday',
        'Wednesday',
        'Thursday',
        'Friday',
        'Saturday',
        'Sunday',
    );

    echo '<tr>';
    for ($i = 0; $i <= 6; $i++) {
        echo '<th>'.$days[$i].'</th>';
    }
    echo '</tr>';

    foreach ($times as $time)
    {
        echo '<tr class="time">';
        for($i = 0; $i <= 6; $i++) {
            echo hasData($getServer, $time, $days[$i], $week, $sID);
        }
        echo '</tr>';
    }
}