1

Recently I posted this question (Converting day of week to dates for the current month with MYSQL & PHP) and got many helpful replies.

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:

enter image description here

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.

1 Answer 1

1

Spent some time educating myself and came up with this (query 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):

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.