gpt4 book ai didi

php - 在表中显示包含周数的数据并附加数据库 php mysql 的数据

转载 作者:行者123 更新时间:2023-11-29 09:52:51 25 4
gpt4 key购买 nike

我发现在 php 中使用 for 循环在表中显示数据时遇到问题。

我使用 bootstrap 作为前端,使用 php mysql 作为后端。

我确实尝试过将数据显示为第一个图像,但我一遍又一遍地得到图像 2 的结果。

即使我尝试使用 css 对其进行样式设置,表格的列和行也不会出现如第一张图片所示这是图像 image1 image2 This is what i want the table to look like

but i ama getting this

预期结果应为 this is the expected outcome下面是我的代码

<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">

<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>

<body>
<?php
$con = mysqli_connect('localhost','root','','reservations');
$startingDay = date('Y-m-d', strtotime("first sunday of this month -6 days"));
$numberOfDays = 35;
$dates = array();
$weekCounter = 1;
$meetingRow = '';

for ($i = 0; $i <= $numberOfDays - 1; $i++) {

$date = date('m/j/Y', strtotime("$startingDay +$i days"));
$dates[] = $date;

if (date('l', strtotime($date)) == "Monday") {

$meetingRow .= '<div class="container">';
$meetingRow .= '<table class="table" border="1" cellpadding="2" cellspacing="2">';
$meetingRow .= '<tbody>';
$meetingRow .= '<tr><td colspan="8"></td></tr>';
$meetingRow .= '<tr>';
$meetingRow .= '<th>Week ' . $weekCounter++ . '</th>';

}

$meetingRow .= '<th>';
$meetingRow .= '<p>' . $date. '</p>';
$meetingRow .= '<p>' . date('l', strtotime($date)). '</p>';
$meetingRow .= '</th>';

if (date('l', strtotime($date)) == "Sunday") {

$meeting_room_sql = "
SELECT id
, meeting_room_name
FROM meeting_room
";
$meeting_room_result = mysqli_query($con, $meeting_room_sql);
// you are to use $meeting_room_result not $meeting_room_sql error occurs in fetching from meeting_room
while ($meeting_room_row = mysqli_fetch_assoc($meeting_room_result)) {

$meeting_room_id = $meeting_room_row['id'];
$meeting_room_name = $meeting_room_row['meeting_room_name'];

$meetingRow .= '<tr>';
$meetingRow .= '<td>';
$meetingRow .= '<div>' . htmlentities($meeting_room_name, ENT_QUOTES, "UTF-8") . '</div>';
$meetingRow .= '<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#meetingModal">Reserve</button>';
$meetingRow .= '</td>';

$reserved_sql = "SELECT meeting_room_id, reserved_date FROM meeting";
$reserved_result = mysqli_query($con, $reserved_sql);
while ($reserved_row = mysqli_fetch_assoc($reserved_result)) {

$reserved_room_id = $reserved_row['meeting_room_id'];
$reserved_date = $reserved_row['reserved_date'];

if ($reserved_room_id == $meeting_room_id) {
$meetingRow .= '<td>' . htmlentities('Meeting Reserved ' . date('m/j/Y', strtotime($reserved_date)), ENT_QUOTES, "UTF-8"). '</td>';
} else {
$meetingRow .= '<td><br /></td>';
}

}
}

$meetingRow .= '<td><br /></td>';
$meetingRow .= '<td><br /></td>';
$meetingRow .= '<td><br /></td>';
$meetingRow .= '<td><br /></td>';
$meetingRow .= '<td><br /></td>';
$meetingRow .= '</tr>';
}

$meetingRow .= '</tbody></tr>';
$meetingRow .= '</table>';
$meetingRow .= '</div>';
}
echo $meetingRow;
?>
<script>
$(function() {
$("#meeting_date").datepicker();
});
</script>
</body>

</html>
{
"meeting_room": {
"Room 1": {
"id":1,
"meeting_room_name":"meeting room 1"
},
"Room 2": {
"id":2,
"meeting_room_name":"meeting room 2"
},
"Room 3": {
"id":3,
"meeting_room_name":"meeting room 3"
},
},
"meeting": {
"1": {
"id":1,
"meeting_room_id":"2",
"reserved_date":"02/02/2019"
},
"2": {
"id":2,
"meeting_room_id":"02/03/2019",
},
"3": {
"id":3,
"meeting_room_id":"01/31/2019",
}
}
}

最佳答案

尝试以下操作,这里的逻辑有很多问题,首先,您每次星期一都会创建新表,因此第一件事是将表的开始和结束标记移出循环。

现在我没有数据库,但我希望它能解决问题。

<!-- language: lang-php -->
<!DOCTYPE html>
<html lang="en">

<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>

<body>
<?php
$con = mysqli_connect('localhost','root','','test');
$startingDay = date('Y-m-d', strtotime("first sunday of this month -6 days"));
$numberOfDays = 35;
$dates = array();
$weekCounter = 1;
$meetingRow = '';
$meetingRow .= '<div class="container">';
$meetingRow .= '<table class="table" border="1" cellpadding="2" cellspacing="2">';
$meetingRow .= '<tbody>';




for ($i = 0; $i <= $numberOfDays - 1; $i++) {

$date = date('m/j/Y', strtotime("$startingDay +$i days"));
$dates[] = $date;

if (date('l', strtotime($date)) == "Monday") {

$meetingRow .= '<tr><td colspan="8"></td></tr>';
$meetingRow .= '<tr>';
$meetingRow .= '<th>Week ' . $weekCounter++ . '</th>';
}

$meetingRow .= '<th>';
$meetingRow .= '<p>' . $date. '</p>';
$meetingRow .= '<p>' . date('l', strtotime($date)). '</p>';
$meetingRow .= '</th>';

if (date('l', strtotime($date)) == "Sunday") {

$meeting_room_sql = "SELECT id , meeting_room_name FROM meeting_room";
$meeting_room_result = mysqli_query($con, $meeting_room_sql);
// you are to use $meeting_room_result not $meeting_room_sql error occurs in fetching from meeting_room
while ($meeting_room_row = mysqli_fetch_assoc($meeting_room_result)) {

$meeting_room_id = $meeting_room_row['id'];
$meeting_room_name = $meeting_room_row['meeting_room_name'];

$meetingRow .= '<tr>
<td>
<div>' . htmlentities($meeting_room_name, ENT_QUOTES, "UTF-8") . '</div>
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#meetingModal">Reserve</button>
</td>';

$reserved_sql = "SELECT meeting_room_id, reserved_date FROM meeting WHERE meeting_room_id = {$meeting_room_row["id"]}";
$reserved_result = mysqli_query($con, $reserved_sql);
$total_number_of_rows = 7;//As we have 7 days in a week so we have 7 days column

$RowCounter = 0;
while ($reserved_row = mysqli_fetch_assoc($reserved_result)) {
$RowCounter++;
$reserved_room_id = $reserved_row['meeting_room_id'];
$reserved_date = $reserved_row['reserved_date'];

if ($reserved_room_id == $meeting_room_id) {
$meetingRow .= '<td>' . htmlentities('Meeting Reserved ' . date('m/j/Y', strtotime($reserved_date)), ENT_QUOTES, "UTF-8"). '</td>';
} else {
$meetingRow .= '<td><br /></td>';
}
}
while ($RowCounter < $total_number_of_rows){
$meetingRow .= '<td><br /></td>';
$RowCounter++;
}

$meetingRow .= '<tr>';

}
$meetingRow .= '</tr>';
}



}
$meetingRow .= '</tbody>';
$meetingRow .= '</table>';
$meetingRow .= '</div>';
echo $meetingRow;
?>
<script>
$(function() {
$("#meeting_date").datepicker();
});
</script>
</body>

</html>

更新 现在,上面的代码工作正常,但召开 session 的逻辑不正确,您应该在该特定日期之前召开 session 。假设如果同一天有两次 session ,则您需要在同一列中添加数据。

enter image description here

关于php - 在表中显示包含周数的数据并附加数据库 php mysql 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54500970/

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