简体   繁体   中英

MySQL and PHP, Comparing two tables, getting the date, and assigning values

I currently have 2 tables. One with employee clockin times and one with their schedules. What I am trying to accomplish is to run this once a day to check to see who was ontime, early, late or absent. Thanks to fabianhjr on my last post I was able to find people who were early or on time using MySQL like so

    $query = "SELECT cms.date, cms.userID, cms.login, oc_schedule.start_time, oc_schedule.userID 
    FROM cms 
    INNER JOIN oc_schedule ON oc_schedule.start_time >= cms.login AND oc_schedule.userID = cms.userID";

Now to find people who were late or absent I have both tables CMS and oc_schedule. CMS is the table that tracks when people have logged in (its a pre-existing system) and oc_schedule which I have just made and input all the schedules in a table with rows userID, Sunday-Saturday with either Y or N as their values, start_time, and end_time.

So what I need to do now is to check CMS for the date, find the name of the day, assign it a value of Y, check in oc_schedule for everyone who has that day with the Value Y and then check for the schedule start_time and if they logged in and if so what time. Move the people who clocked in on time or early into another table, and every one who was late or absent into the late_absent table.

So far this is as far as I have gotten for the late and absent list. Right now all it does is find logins that are either null or greater then the schedule time but it doesnt really work without it comparing the dates. And I am echoing the tables right now just to see that it works without having to post to the Database.

    <?php 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("sacc") or die(mysql_error()); 

    $query = "SELECT cms.date, cms.userID, cms.login, oc_schedule.start_time, oc_schedule.userID, oc_schedule.sunday, oc_schedule.monday, oc_schedule.tuesday, oc_schedule.wednesday, oc_schedule.thursday, oc_schedule.friday, oc_schedule.saturday
    FROM cms
    RIGHT OUTER JOIN oc_schedule
    ON oc_schedule.userID = cms.userID AND oc_schedule.start_time < cms.login";

    $result = mysql_query($query) or die(mysql_error());

    echo "<table cellpadding='5' border='0' style='border: 1px solid black;border-collapse:collapse'>
    <tr>
    <th style='border: 1px solid black;'>Date</th>
    <th style='border: 1px solid black;'>CID/WIN</th>
    <th style='border: 1px solid black;'>Scheduled Login</th>
    <th style='border: 1px solid black;'>Late Login Time</th>
    </tr>";

    while($row = mysql_fetch_array($result)) {

    echo "<tr style='text-align: center;'>";
    echo "<td style='border: 1px solid black;'>" . $row['date'] . "</td>";
    echo "<td style='border: 1px solid black;'>" . $row['userID'] . "</td>";
    echo "<td style='border: 1px solid black;'>" . $row['start_time'] . "</td>";
    echo "<td style='border: 1px solid black;'>" . $row['login'] . "</td>";
    echo "</tr>";

    }
    echo "</table>";
    ?>

Any help would be greatly appreciated. Thanks.

Without testing it, this could be an answer. It gives a table with a header per type (early/ontime/late/absent), followed by all records that are that type.

$query  = "SELECT ";
$query .=     "cms.date, cms.userid, cms.login, ";
$query .=     "s.start_time, s.sunday, s.monday, s.tuesday, s.wednesday, s.thursday, s.friday, s.saturday ";
$query .= "FROM ";
$query .=     "cms ";
$query .=     "RIGHT OUTER JOIN oc_schedule s ON s.userid = cms.userid ";
$query .= "ORDER BY ";
$query .=     "(s.start_time - cms.login) AS offset";

$result = mysql_query($query) or die(mysql_error());

$lasttype = "";

while($row = mysql_fetch_array($result)) {
    if($row['cms.login'] == ""){
        $curtype = 'Absent';
    } else {
        switch($row['offset']){
            case > 0: $curtype = 'Early'; break;
            case < 0: $curtype = 'Late'; break;
            case 0: $curtype = 'On Time'; break;
        }
    }

    if($curtype != $lasttype){
        ?>
            <tr>
                <th colspan="4"><?=$curtype?></th>
            </tr>
        <?
    }

    ?>
        <tr>
            <td><?=$row['date']?></td>
            <td><?=$row['userid']?></td>
            <td><?=$row['start_time']?></td>
            <td><?=$row['login']?></td>

        </tr>
    <?

    $lasttype = $curtype;
}

Example:

ABSENT
20-jan-12 123 09:00:00 NULL
20-jan-12 124 09:00:00 NULL
LATE
20-jan-12 125 09:00:00 09:20:00
20-jan-12 126 09:00:00 09:05:00
ON TIME
20-jan-12 127 09:00:00 09:00:00
EARLY
20-jan-12 128 09:00:00 08:50:00
20-jan-12 129 09:00:00 08:30:00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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