Untitled

 avatar
unknown
plain_text
a month ago
2.7 kB
6
Indexable
// Backend: Fetch User's Time-In and Time-Out for the Current Day


<?php
// Assuming you have a connection to your DB
session_start();
$user = $_SESSION['username'];  // Get the logged-in user

// Get today's date (formatted YYYY-MM-DD)
$today = date('Y-m-d');

// Query to fetch time-in and time-out records for today
$query = "SELECT * FROM attendance WHERE username = ? AND DATE(time_in) = ?";
$stmt = $pdo->prepare($query);
$stmt->execute([$user, $today]);
$attendance = $stmt->fetchAll();

// Check if there are records for today
if (count($attendance) > 0) {
    $timeIn = $attendance[0]['time_in'];
    $timeOut = $attendance[0]['time_out'];
    $undertime = $attendance[0]['undertime']; // Assuming you're storing undertime
} else {
    $timeIn = $timeOut = $undertime = null;
}
?>




// Frontend: Display the Attendance Records in the Table




<tbody id="attendance-record">
    <!-- Records will be injected here by JavaScript -->
</tbody>

<script>
    // Sample data fetched from PHP
    const timeIn = "<?php echo $timeIn; ?>";
    const timeOut = "<?php echo $timeOut; ?>";
    const undertime = "<?php echo $undertime; ?>";
    
    // DOM elements for buttons
    const timeInBtn = document.getElementById('time-in-btn');
    const timeOutBtn = document.getElementById('time-out-btn');
    
    // DOM element for the attendance table
    const attendanceRecord = document.getElementById('attendance-record');
    
    // Function to display the records in the table
    function displayAttendanceRecord() {
        // Check if there's a record for today
        if (timeIn && timeOut) {
            // Create row for the time-in and time-out
            const recordRow = `
                <tr>
                    <td>TIME IN</td>
                    <td>${timeIn}</td>
                    <td>${undertime ? undertime : ''} minutes</td>
                </tr>
                <tr>
                    <td>TIME OUT</td>
                    <td>${timeOut}</td>
                    <td>${undertime ? undertime : ''} minutes</td>
                </tr>
            `;
            attendanceRecord.innerHTML = recordRow;

            // Disable buttons if record exists
            timeInBtn.disabled = true;
            timeOutBtn.disabled = true;
        } else {
            // Display no records message if no attendance records
            attendanceRecord.innerHTML = `
                <tr>
                    <td colspan="3">No time-in and time-out recorded for today.</td>
                </tr>
            `;
        }
    }

    // Call the function on page load
    displayAttendanceRecord();
</script>

Editor is loading...
Leave a Comment