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

时间:2015-05-21 13:35:48

标签: php calendar

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

enter image description here

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

enter image description here

代码:

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>';
    }
} 

1 个答案:

答案 0 :(得分:1)

我对您的代码进行了一些更改,但让它发挥作用。

关键是hasData()函数循环,如果找到数据,它会返回一个打破循环的函数。

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

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>';
    }
}