gpt4 book ai didi

PHP Fetch - 不返回所有行进行数组检查

转载 作者:行者123 更新时间:2023-11-29 10:33:37 25 4
gpt4 key购买 nike

我正在开发按小时计费的预订系统。根据所选的日期,我设置了一个 JQuery 函数来更改表单上的时间选择。

作为一个测试,我只是想列出可用和已采取的时间。

我遇到的问题是,我想要根据所选日期显示的时间值(21:00 和 22:00)无法正常工作。

21:00 正确拾取为正在拍摄。 22:00 未被正确拾取。

我的表有以下设置和测试数据:

ID | NAME    | DAY | TIME  | HOTEL | HOST
1 | Event 1 | 2 | 21:00 | South | Joe
2 | Event 2 | 2 | 22:00 | South | Matt

在我的事件预订页面上,我有以下内容:

更改时间选择的脚本:

<script>
function getData()
{
var formData = new FormData($("#eventbook")[0]);
$.ajax({
url: 'eventdata.php',
type:'POST',
data: formData,
processData: false,
contentType: false,
cache: false,
mimeType: 'multipart/form-data',
success: function(html)
{
$("#eventitems").html(html);
}
});
return false
}
</script>

表格:

<form id="eventbook" action="">
<div class="form-group">
<p class="text-center">
<label for="selOption">Event Day:</label>
<select class="form-control" id="selOption" onchange="getData()" name="selOption">
<option value="1">Monday</option>
<option value="2">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
<option value="6">Saturday</option>
<option value="7">Sunday</option>
</select>
</p>
</div>
<div id="eventitems">
<?php
$times = array(
"00:00" => "00:00",
"01:00" => "01:00",
"02:00" => "02:00",
"03:00" => "03:00",
"04:00" => "04:00",
"05:00" => "05:00",
"06:00" => "06:00",
"07:00" => "07:00",
"08:00" => "08:00",
"09:00" => "09:00",
"10:00" => "10:00",
"11:00" => "11:00",
"12:00" => "12:00",
"13:00" => "13:00",
"14:00" => "14:00",
"15:00" => "15:00",
"16:00" => "16:00",
"17:00" => "17:00",
"18:00" => "18:00",
"19:00" => "19:00",
"20:00" => "20:00",
"21:00" => "21:00",
"22:00" => "22:00",
"23:00" => "23:00",
);
if (!isset($_REQUEST['selOption'])) {
$day = 1;
}
$dayInfo = $dbh->prepare("SELECT * FROM events WHERE day=:day");
$dayInfo->execute(array(":day"=>$day));
$dayInfoResult = $dayInfo->fetch(PDO::FETCH_ASSOC);
?>
<div class="form-group">
<p class="text-center">
<?php
foreach ($times as $key => $value) {
if (in_array($value, $dayInfoResult)) {
echo "{$key} with value of {$value} is taken.<br>";
} else {
echo "{$key} with value of {$value} is available.<br>";
}
}
?>
</p>
</div>
</div>
</form>

在 eventdata.php 中我有以下内容:

<?php
require_once 'includes/config.php';
$times = array(
"00:00" => "00:00",
"01:00" => "01:00",
"02:00" => "02:00",
"03:00" => "03:00",
"04:00" => "04:00",
"05:00" => "05:00",
"06:00" => "06:00",
"07:00" => "07:00",
"08:00" => "08:00",
"09:00" => "09:00",
"10:00" => "10:00",
"11:00" => "11:00",
"12:00" => "12:00",
"13:00" => "13:00",
"14:00" => "14:00",
"15:00" => "15:00",
"16:00" => "16:00",
"17:00" => "17:00",
"18:00" => "18:00",
"19:00" => "19:00",
"20:00" => "20:00",
"21:00" => "21:00",
"22:00" => "22:00",
"23:00" => "23:00",
);
$day = $_REQUEST['selOption'];
$dayInfo = $dbh->prepare("SELECT * FROM events WHERE day=:day");
$dayInfo->execute(array(":day"=>$day));
$dayInfoResult = $dayInfo->fetch(PDO::FETCH_ASSOC);
?>
<div class="form-group">
<p class="text-center">
<?php
foreach ($times as $key => $value) {
if (in_array($value, $dayInfoResult)) {
echo "{$key} with value of {$value} is taken.<br>";
} else {
echo "{$key} with value of {$value} is available.<br>";
}
}
?>
</p>
</div>

选择星期一、星期三、星期四、星期五、星期六或星期日后,所有时间都正确显示为“值为 VALUE 的 KEY 可用。”。

选择星期二后,仅显示拍摄时间为 21:00,而不显示 22:00。

最佳答案

您需要使用 fetchall 而不是 fetch。

替换

$dayInfoResult = $dayInfo->fetch(PDO::FETCH_ASSOC);

$dayInfoResult = $dayInfo->fetchAll(PDO::FETCH_ASSOC);

fetch:从结果集中获取下一行。

fetchAll :返回包含所有结果集行的数组

并且还需要修改代码来检查所花费的时间。

// fetch all data from database for that day
$dayInfoResult = $dayInfo->fetchAll(PDO::FETCH_ASSOC);
// define an array
$taken_times = array();
// loop the database results
foreach($dayInfoResult as $key=>$value){
// new array to get all the taken times for that day
$taken_times[] = $value['TIME'];
}

在你的 html 代码中

foreach ($times as $key => $value) {
// check the time in new array
if (in_array($value, $taken_times)) {
echo "{$key} with value of {$value} is taken.<br>";
} else {
echo "{$key} with value of {$value} is available.<br>";
}
}

关于PHP Fetch - 不返回所有行进行数组检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46919135/

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