简体   繁体   English

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

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

Recently I posted this question ( Converting day of week to dates for the current month with MYSQL & PHP ) and got many helpful replies.最近我发布了这个问题( 使用 MYSQL 和 PHP 将星期几转换为当月的日期)并得到了许多有用的答复。

I'd like to build on this and do something more but again am reaching the limits of my knowledge and am looking for some guidance.我想以此为基础,做更多的事情,但又一次达到了我的知识极限,正在寻找一些指导。

Purpose: I have weekly recurring events/attendance, which I am able to match and display with the date of the current month thanks to above.目的:我有每周重复发生的事件/出勤,由于上述原因,我能够将其与当月的日期匹配并显示。 Also, I have a database of cancellations/date changes to these weekly recurring events/attendance.此外,我有这些每周定期活动/出勤的取消/日期更改数据库。

I'd like to match these two to display in one table.我想将这两个匹配以显示在一张表中。

Here's the current situation:这是目前的情况:

在此处输入图片说明

As you can see I'm making this for mobile.正如你所看到的,我正在为移动设备做这个。 Currently I show the set schedule on top with the bottom displaying the cancellations (in red) and the one-off reservations to other events (in green).目前我在顶部显示设定的时间表,底部显示取消(红色)和对其他事件的一次性预订(绿色)。

Here's my code for the first part:这是我的第一部分代码:

$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>";
}

And code for the second part:以及第二部分的代码:

$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>";
        }

    }
}

The first set of data is being stored in an array.第一组数据被存储在一个数组中。 I assume what I need to do is input things into an array in the second stage and then then compare the information in those two arrays, merge them into one, have another array corresponding to the first which tags the data in the first with things like "absent," then output the the new arrays in a master list?我假设我需要做的是在第二阶段将事物输入到一个数组中,然后比较这两个数组中的信息,将它们合并为一个,有另一个与第一个数组相对应的数组,它用类似的东西标记第一个数组中的数据“不存在”,然后在主列表中输出新数组?

Is this right?这是正确的吗? I'm having trouble conceptualizing how to make this code work.我在概念化如何使这段代码工作时遇到了麻烦。

Thanks in advance.提前致谢。

Spent some time educating myself and came up with this (query 1):花了一些时间自学并想出了这个(查询 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>";
}

(query 2): (查询 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>";
        }

    }
}

This is probably not the cleanest way to do it, but it works.这可能不是最干净的方法,但它有效。 I put the dates into an array with the key set to the date and the value set to some number referencing attendance (attending, absent, uncertain).我将日期放入一个数组中,键设置为日期,值设置为一些引用出勤率的数字(出席、缺席、不确定)。 Dates from both queries (the regular attendance results and the irregular reservations) are put into the array, then reordered by date using ksort, then output based on the reservation status.将来自两个查询(常规考勤结果和不规则预约)的日期放入数组,然后使用 ksort 按日期重新排序,然后根据预约状态输出。

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

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