gpt4 book ai didi

php - 日历系统预约报错,PHP循环出错

转载 作者:搜寻专家 更新时间:2023-10-31 21:30:40 24 4
gpt4 key购买 nike

目前我正在尝试使预订系统看起来像这样(用 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>';
}
}

最佳答案

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

关键是循环的 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>';
}
}

关于php - 日历系统预约报错,PHP循环出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30375355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com