gpt4 book ai didi

php - 使用 PHP 比较和输出来自数组和 MySQL 查询的日期

转载 作者:行者123 更新时间:2023-11-28 23:21:03 25 4
gpt4 key购买 nike

最近我发布了这个问题(Converting day of week to dates for the current month with MYSQL & PHP)并得到了许多有用的回复。

我想在此基础上做更多的事情,但又一次达到了我的知识极限,我正在寻找一些指导。

目的:我每周都有定期事件/出勤,由于上述原因,我可以将其与当月的日期进行匹配和显示。此外,我有一个数据库,其中包含这些每周重复发生的事件/出勤的取消/日期更改。

我想将这两个匹配显示在一个表中。

这是目前的情况:

enter image description here

如您所见,我正在为移动设备制作此内容。目前,我在顶部显示设定的时间表,在底部显示取消(红色)和对其他事件的一次性预订(绿色)。

这是我的第一部分代码:

$sql = "SELECT StudentDB.studentid, ClassDB.classID, ClassDB.class_level, ClassDB.class_title, ClassDB.time, ClassDB.teacher, StudentDB.first_name, StudentDB.last_name, StudentDB.payment_amount, ClassDB.day 
FROM ClassDB
INNER JOIN RegDB ON ClassDB.classID = RegDB.classid
INNER JOIN StudentDB ON StudentDB.studentID = RegDB.studentid
WHERE StudentDB.studentid = '$studentid'";


$result = $conn->query($sql);



if ($result->num_rows > 0) {
echo "<table class='table table-hover'>";
echo "<tr><th colspan=2 class='text-center'>固定レッスン</th></tr><tr><th>クラス</th><th>開始時間</th></tr>";


// output data of each row
while($row = $result->fetch_assoc()) {

$dayofclass = $row['day'];
echo "<tr><td>".$row["class_level"]." ".$row["class_title"]."</td><td>".$row['day']." ".$row['class_time']." " .$row['time']. "</td></tr>";

}

$n=date('t',strtotime($date)); // find no of days in this month

$yearmonth = date("Y-m-");

$dates=array();
for ($i=1;$i<=$n;$i++){

if ($i<10) {
$i = "0" . $i;
}

$day=date("l",strtotime($yearmonth.$i)); //find weekdays


if($day==$dayofclass){
$dates[]=$yearmonth.$i;
}
}

$arrlength = count($dates);
for($x = 0; $x < $arrlength; $x++) {
$y = $x +1;
echo "<tr><td>Week ".$y."</td><td>".$dates[$x]."</td></tr>";
}



echo "</table>";
}

第二部分的代码:

$sql = "SELECT AttendanceDB.*, ClassDB.* 
FROM StudentDB
INNER JOIN AttendanceDB ON StudentDB.studentid = AttendanceDB.studentid
INNER JOIN ClassDB ON AttendanceDB.classid = ClassDB.classID
WHERE StudentDB.studentid = '$studentid' AND AttendanceDB.class_time >= '$date'";



$result = $conn->query($sql);



if ($result->num_rows > 0) {
echo "<table class='table table-hover'>";
echo "<tr><th colspan=2 class='text-center'>振替の予定</th></tr><tr><th>クラス</th><th>開始時間</th></tr>";


// output data of each row
while($row = $result->fetch_assoc()) {

$phpdate = strtotime( $row["class_time"] );
$mysqldate = date( 'Y-m-d', $phpdate );

if ($row["furikae"] == 3){
echo "<tr class=success><td>振替(".$row["class_level"]." ".$row["class_title"].")</td><td>".$mysqldate." " .$row['time']. "</td></tr>";
} elseif ($row["furikae"] == 8) {
echo "<tr class=warning><td>承認待ち</td><td>".$mysqldate." " .$row['time']. "</td></tr>";
} elseif ($row["furikae"] == 2) {
echo "<tr class=danger><td>休み</td><td>".$mysqldate." " .$row['time']. "</td></tr>";
}

}
}

第一组数据被存储在一个数组中。我假设我需要做的是在第二阶段将东西输入到一个数组中,然后比较这两个数组中的信息,将它们合并成一个,让另一个数组对应于第一个数组,它用类似的东西标记第一个数组中的数据“不存在”,然后在主列表中输出新数组?

这样对吗?我在概念化如何使这段代码工作时遇到问题。

提前致谢。

最佳答案

花了一些时间自学并想出了这个(查询 1):

if ($result->num_rows > 0) {
echo "<table class='table table-hover'>";
echo "<tr><th colspan=2 class='text-center'>固定レッスン</th></tr><tr><th>クラス</th><th>開始時間</th></tr>";


// output data of each row
while($row = $result->fetch_assoc()) {

$dayofclass = $row['day'];
echo "<tr><td>".$row["class_level"]." ".$row["class_title"]."</td><td>".$row['day']." ".$row['class_time']." " .$row['time']. "</td></tr>";

}

$n=date('t',strtotime($date)); // find no of days in this month

$yearmonth = date("Y-m-");

$dates=array();
for ($i=1;$i<=$n;$i++){

if ($i<10) {
$i = "0" . $i;
}

$day=date("l",strtotime($yearmonth.$i)); //find weekdays


if($day==$dayofclass){
$dates[]=$yearmonth.$i;
$datesdata[$yearmonth.$i] = "0";
}
}





echo "</table>";
}

(查询 2):

if ($result->num_rows > 0) {
echo "<table class='table table-hover'>";
echo "<tr><th colspan=2 class='text-center'>今月の予定</th></tr><tr><th>クラス</th><th>開始時間</th></tr>";


// output data of each row
while($row = $result->fetch_assoc()) {

$phpdate = strtotime( $row["class_time"] );
$mysqldate = date( 'Y-m-d', $phpdate );



if ($row["furikae"] == 3){
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "1";
} elseif ($row["furikae"] == 8) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "3";
} elseif ($row["furikae"] == 2) {
$dates[]=$mysqldate;
$datesdata[$mysqldate] = "2";
}

}



ksort($datesdata);
foreach ($datesdata as $key => $val) {
if ($val == 0){
echo "<tr><td>参加予定</td><td>".$key."</td></tr>";
} elseif ($val == 1) {
echo "<tr class='success'><td>振替参加予定</td><td>".$key."</td></tr>";
} elseif ($val == 2) {
echo "<tr class='danger'><td>休み予定</td><td>".$key."</td></tr>";
} elseif ($val == 3) {
echo "<tr class='warning'><td>キャンセル待ち</td><td>".$key."</td></tr>";
}

}
}

这可能不是最简洁的方法,但它确实有效。我将日期放入一个数组中,键设置为日期,值设置为一些引用出勤的数字(出席、缺席、不确定)。将两个查询(常规考勤结果和非定期预订)的日期放入数组,然后使用 ksort 按日期重新排序,然后根据预订状态输出。

关于php - 使用 PHP 比较和输出来自数组和 MySQL 查询的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41560935/

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