How to use PHP to develop employee attendance data visualization tool?
In modern enterprises, employee attendance management is an extremely important task. In order to better manage and analyze employee attendance, many companies have adopted data visualization tools to display and count employee attendance data. This article will introduce how to use PHP to develop an employee attendance data visualization tool and provide specific code examples.
First of all, we need to clarify the functions and data structures to be implemented. Generally speaking, employee attendance data includes information such as work number, date, work time, get off work time, number of late arrivals, and number of early departures. We need to store this data in a database and use PHP for data query and visualization.
First, create a database named "employee_attendance", and then create a data table named "attendance" to store employee attendance data . The structure of the data table is as follows:
CREATE TABLE attendance ( id INT PRIMARY KEY AUTO_INCREMENT, employee_id INT, date DATE, start_time TIME, end_time TIME, late_count INT, early_leave_count INT );
Enter the corresponding attendance data into the database every time an employee goes to work or leaves work. Data entry functions can be implemented using HTML forms and PHP code.
HTML code example:
<form action="submit_attendance.php" method="POST"> <label for="employee_id">工号:</label> <input type="text" name="employee_id" id="employee_id" required> <label for="date">日期:</label> <input type="date" name="date" id="date" required> <label for="start_time">上班时间:</label> <input type="time" name="start_time" id="start_time" required> <label for="end_time">下班时间:</label> <input type="time" name="end_time" id="end_time" required> <label for="late_count">迟到次数:</label> <input type="number" name="late_count" id="late_count" min="0" required> <label for="early_leave_count">早退次数:</label> <input type="number" name="early_leave_count" id="early_leave_count" min="0" required> <input type="submit" value="提交"> </form>
PHP code example (submit_attendance.php):
<?php $employee_id = $_POST['employee_id']; $date = $_POST['date']; $start_time = $_POST['start_time']; $end_time = $_POST['end_time']; $late_count = $_POST['late_count']; $early_leave_count = $_POST['early_leave_count']; // 连接数据库 $conn = mysqli_connect("localhost", "root", "password", "employee_attendance"); // 插入数据 $query = "INSERT INTO attendance (employee_id, date, start_time, end_time, late_count, early_leave_count) VALUES ('$employee_id', '$date', '$start_time', '$end_time', '$late_count', '$early_leave_count')"; mysqli_query($conn, $query); mysqli_close($conn); ?>
Next, We will use PHP and Chart.js library to visualize employee attendance data. First, we need to obtain employee attendance data from the database and conduct statistics and grouping according to certain rules.
PHP code example:
<?php // 连接数据库 $conn = mysqli_connect("localhost", "root", "password", "employee_attendance"); // 查询数据 $query = "SELECT employee_id, SUM(late_count) as total_late, SUM(early_leave_count) as total_early_leave FROM attendance GROUP BY employee_id"; $result = mysqli_query($conn, $query); $attendance_data = []; while ($row = mysqli_fetch_assoc($result)) { $employee_id = $row['employee_id']; $total_late = $row['total_late']; $total_early_leave = $row['total_early_leave']; $attendance_data[] = [ "employee_id" => $employee_id, "total_late" => $total_late, "total_early_leave" => $total_early_leave ]; } mysqli_close($conn); ?>
Then, we can use the Chart.js library to create a histogram to visualize the statistical results.
HTML code example:
<!DOCTYPE html> <html> <head> <title>员工考勤数据可视化</title> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> </head> <body> <canvas id="attendanceChart"></canvas> <script> // 获取考勤数据 var attendanceData = <?php echo json_encode($attendance_data); ?>; // 数据处理 var employeeIds = []; var totalLateCounts = []; var totalEarlyLeaveCounts = []; attendanceData.forEach(function(data) { employeeIds.push(data.employee_id); totalLateCounts.push(data.total_late); totalEarlyLeaveCounts.push(data.total_early_leave); }); // 创建柱状图 var ctx = document.getElementById('attendanceChart').getContext('2d'); var chart = new Chart(ctx, { type: 'bar', data: { labels: employeeIds, datasets: [{ label: '迟到次数', data: totalLateCounts, backgroundColor: 'rgba(255, 99, 132, 0.2)', borderColor: 'rgba(255, 99, 132, 1)', borderWidth: 1 }, { label: '早退次数', data: totalEarlyLeaveCounts, backgroundColor: 'rgba(54, 162, 235, 0.2)', borderColor: 'rgba(54, 162, 235, 1)', borderWidth: 1 }] }, options: { scales: { y: { beginAtZero: true } } } }); </script> </body> </html>
The above are the specific steps and code examples for using PHP to develop employee attendance data visualization tools. By entering employee attendance data and statistics and using the Chart.js library for visualization, we can have a clearer understanding of employee attendance and provide strong support for the company's management decisions.
The above is the detailed content of How to use PHP to develop employee attendance data visualization tool?. For more information, please follow other related articles on the PHP Chinese website!