Home > Backend Development > PHP Tutorial > How to implement summary statistics of employee attendance data in PHP?

How to implement summary statistics of employee attendance data in PHP?

WBOY
Release: 2023-09-24 17:36:02
Original
1326 people have browsed it

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:

  • id: employee ID, type integer
  • name: employee name, type It is a string
  • date: attendance date, type is date
  • time_in: working time, type is time
  • time_out: off-duty time, type is time

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 "连接成功";
?>
Copy after login

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();
?>
Copy after login

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();
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template