繁体   English   中英

使用 PHP 从数组和 MySQL 查询中比较和输出日期

[英]Comparing and outputting dates from an array and a MySQL query using PHP

最近我发布了这个问题( 使用 MYSQL 和 PHP 将星期几转换为当月的日期)并得到了许多有用的答复。

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

目的:我有每周重复发生的事件/出勤,由于上述原因,我能够将其与当月的日期匹配并显示。 此外,我有这些每周定期活动/出勤的取消/日期更改数据库。

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

这是目前的情况:

在此处输入图片说明

正如你所看到的,我正在为移动设备做这个。 目前我在顶部显示设定的时间表,底部显示取消(红色)和对其他事件的一次性预订(绿色)。

这是我的第一部分代码:

$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 按日期重新排序,然后根据预约状态输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM