我正在尝试创建一个表格,显示今天日期的房间可用性。
我有一个MySQL表,标题为'start_time''end_time'和'date'。我需要在排序like this的时间表中显示可用的时隙。我尝试了很多方法,但我仍然缺少一些东西。我不认为我正在创建一个数组并正确地循环它。以下是我到目前为止:
<?php
define('DB_NAME', 'database');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn) {
die('Could not connect: ' . mysqli_connect_error());
}
else { echo "Connected"; };
$roomsAvailability = 'SELECT start_time, end_time FROM room_booking WHERE date = CURDATE()';
$retval = mysqli_query( $conn, $roomsAvailability);
if(! $retval ) {
die('Could not get data: ' . mysqli_connect_error());
}
$times = ['9:00','9:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00',
'15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30', '19:00', '19:30', '20:00'];
?>
<style>
table, th, td {
margin:5px;
border: 1px solid black;
}
.available {
background-color: lightblue;
}
</style>
<table>
<tr><th> </th><th>B-12</th><th>G.05</th><th>G.06</th><th>1.04</th><th>1.05</th><th>1.06</th><th>2.04</th><th>2.05</th><th>2.06</th><th>3.04</th><th>3.05</th><th>3.06</th></tr>
<?php
foreach($times as $time)
{
?>
<tr>
<?php
echo "<td>$time</td>";
foreach($roomsAvailability as $room) => $roomsAvailability)
{
if(in_array($time , $roomsAvailability))
{
echo "<td class='available'> </td>";
}
else
{
echo "<td class='notAvailable'> </td>";
}
}
?>
</tr>
<?php
}
?>
</table>
任何人都可以在这看到我做错了吗?我收到错误:为foreach()提供的参数无效
如果有人能够发现我做错了什么或知道更好的方法,我会非常感激。
答案 0 :(得分:0)
您的foreach未正确定义。
this.x
根据:http://php.net/manual/en/control-structures.foreach.php
foreach($roomsAvailability as $room) => $roomsAvailability)
希望这有帮助!
答案 1 :(得分:0)
您的代码中有几处错误,
首先,您需要在foreach
中执行之前将数据作为数组获取$roomsAvailability = 'SELECT start_time, end_time FROM room_booking WHERE date = CURDATE()';
$retval = mysqli_query( $conn, $roomsAvailability);
$results = array();
// fetch all records with while loop
while($row = mysqli_fetch_array($retval)) {
$results[] = $row;
}
那么你的foreach语法错误应该是
foreach($results as $room => $roomsAvailability)
{
if(in_array($time , $roomsAvailability))
{
---
}
}
注意:$结果为mysqli_fetch_array
值mysql fetch array
答案 2 :(得分:0)
首先,错误在于下面给出的部分
$retval = mysqli_query( $conn, $roomsAvailability);
if(! $retval ) { --- }
你没有告诉php如何处理mysqli_query()函数返回的resource_id。对于if语句,它将始终返回true,直到函数返回resource_id。
其次,你的foreach是不正确的。你的陈述是
foreach($roomsAvailability as $room) => $roomsAvailability)
foreach的语法是
foreach($_your_variable as $_new_variable)
或者,如果您有兴趣获取索引以及存储在该索引中的值,则可以使用以下给定的语法。
foreach($_your_variable as $_index => $_value)
您可以指定任何变量名称,而不是$ _index和$ _value。
第三,变量$ roomsAvailability包含sql字符串。所以你不能以mysql资源的形式访问它。你应该在$ retval上使用mysqli_fetch_object(),mysqli_fetch_array()或mysqli_fetch_assoc(),因为它包含mysql查询的有效resource_id。代码可能类似于下面的代码
$roomsAvailability = 'SELECT start_time, end_time FROM room_booking WHERE date = CURDATE()';
$retval = mysqli_query( $conn, $roomsAvailability);
$result = mysqli_fetch_assoc($retval);
foreach($result as $row){
--Your Statements--
}