How to format the output of employee attendance data through PHP?

PHPz
Release: 2023-09-24 11:32:01
Original
658 people have browsed it

How to format the output of employee attendance data through PHP?

How to format the output of employee attendance data through PHP?

Attendance is an important part of enterprise management. Monitoring and counting employees' working hours and work conditions can help enterprises improve work efficiency and scientific management. In practical applications, we often need to output employee attendance data in a certain format to facilitate manual analysis and machine processing. This article will introduce how to format the output of employee attendance data through PHP and give corresponding code examples.

1. Preparation
Before starting, we need to prepare the source data of employee attendance data. Generally speaking, attendance data includes the employee's name, department, attendance date and attendance time, which can be stored in the database. Here, we assume that there is already a data table named attendance, with the following structure:

CREATE TABLE attendance (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    department VARCHAR(50) NOT NULL,
    date DATE NOT NULL,
    time TIME NOT NULL
);
Copy after login

2. Connect to the database
First, we need to connect to the database to obtain attendance data. You can use PHP's built-in PDO extension to connect to the MySQL database. The following is the code to connect to the database:

<?php
$dsn = "mysql:host=localhost;dbname=database";
$username = "username";
$password = "password";

try {
    $pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    die("数据库连接失败:" . $e->getMessage());
}
Copy after login

In actual application, database, username and password need to be replaced with the correct database name ,user name and password.

3. Query data
After connecting to the database, we can execute the query statement to obtain the attendance data. The following is a code example to obtain data:

<?php
$sql = "SELECT * FROM attendance";
$stmt = $pdo->query($sql);
$attendanceData = $stmt->fetchAll(PDO::FETCH_ASSOC);
Copy after login

The above code will query all the data in the attendance table and store it in the attendanceData array.

4. Formatted output
After obtaining the data, we can format the attendance data and output it according to the needs. The following is a code example that groups attendance data by department and sorts it by date and time:

<?php
// 按照部门分组
$departmentData = [];
foreach ($attendanceData as $attendance) {
    $department = $attendance['department'];
    if (!isset($departmentData[$department])) {
        $departmentData[$department] = [];
    }
    $departmentData[$department][] = $attendance;
}

// 按照日期和时间排序
foreach ($departmentData as &$department) {
    usort($department, function ($a, $b) {
        return strcmp($a['date'] . $a['time'], $b['date'] . $b['time']);
    });
}

// 格式化输出
foreach ($departmentData as $department => $attendances) {
    echo "$department 考勤数据:
";
    foreach ($attendances as $attendance) {
        $name = $attendance['name'];
        $date = $attendance['date'];
        $time = $attendance['time'];
        echo "姓名:$name    日期:$date    时间:$time
";
    }
    echo "
";
}
Copy after login

The above code groups the attendance data by department and sorts the attendance data of each department by date and time. Sort and then format the output.

Summary
Through the above steps, we can format and output employee attendance data through PHP. In practical applications, the output can be further processed and optimized according to specific needs. I hope this article can help you understand and apply the formatted output of employee attendance data.

The above is the detailed content of How to format the output of employee attendance data through 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!