Home > Article > Backend Development > How to implement summary statistics of employee attendance data in PHP?
How to implement summary statistics of employee attendance data in PHP?
Introduction:
In an enterprise, employee attendance data is very important for management and payroll accounting. In order to more conveniently analyze and count employee attendance, we can use PHP to implement summary statistics of employee attendance data. This article will introduce how to implement this function through PHP programming and provide specific code examples.
Step 1: Create database and table
First, we need to create a table in the database to store employee attendance data. In this example, we create a database named "attendance" with the following fields:
This table will be used to store employees' daily attendance records.
Step 2: Connect to the database
In the PHP code, we need to connect to the database first. Database connections can be achieved using the mysqli extension. The following code example shows how to connect to the database:
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "attendance"; // 创建连接 $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功"; ?>
Step 3: Implement data insertion
When employees go to work and leave work, attendance data needs to be inserted into the database. The following code example shows how to insert attendance data into the "attendance" table of the database:
<?php // 获取员工ID、姓名、考勤日期、上班时间和下班时间 $id = $_POST['id']; $name = $_POST['name']; $date = $_POST['date']; $time_in = $_POST['time_in']; $time_out = $_POST['time_out']; // 准备插入数据的SQL语句 $sql = "INSERT INTO attendance (id, name, date, time_in, time_out) VALUES ('$id', '$name', '$date', '$time_in', '$time_out')"; // 执行SQL语句 if ($conn->query($sql) === TRUE) { echo "考勤数据插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } // 关闭数据库连接 $conn->close(); ?>
Step 4: Statistics of attendance data
To achieve summary statistics of attendance data, we can use SQL statements to query the database data in it, grouping and calculating as needed. The following code example shows how to query the attendance data of a certain month in the database and count the number of attendance days of each employee:
<?php // 获取要统计的月份 $month = $_POST['month']; // 准备查询的SQL语句 $sql = "SELECT name, COUNT(*) AS attendance_days FROM attendance WHERE MONTH(date) = '$month' GROUP BY name"; // 执行SQL查询 $result = $conn->query($sql); // 输出查询结果 if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "员工:" . $row["name"]. "出勤天数:" . $row["attendance_days"]. "<br>"; } } else { echo "暂无考勤数据"; } // 关闭数据库连接 $conn->close(); ?>
Through the above steps, we can achieve summary statistics of employee attendance data. You can further expand and optimize this function according to specific needs. Hope this article can be helpful to you!
The above is the detailed content of How to implement summary statistics of employee attendance data in PHP?. For more information, please follow other related articles on the PHP Chinese website!