Home > Backend Development > PHP Tutorial > How to generate online employee attendance report via PHP and Vue

How to generate online employee attendance report via PHP and Vue

WBOY
Release: 2023-09-24 16:20:01
Original
816 people have browsed it

How to generate online employee attendance report via PHP and Vue

How to generate online employee attendance report through PHP and Vue

In the modern office environment, managing employee attendance is a very important task. With the continuous development of technology, generating online employee attendance reports through automated systems has become a common requirement. This article will introduce how to use PHP and Vue to implement this function, and provide specific code examples.

  1. Preliminary preparation
    Before starting, we need to ensure that the relevant environments of PHP and Vue have been installed on the server. If it is not installed, you can find the corresponding installation tutorial through the link below.
  • PHP installation tutorial: https://www.php.net/manual/en/install.php
  • Vue installation tutorial: https://vuejs .org/v2/guide/installation.html
  1. Create database table
    Before generating the attendance report, we need to create a database table to store employee attendance records. You can use the following SQL statement to create a simple table structure.
CREATE TABLE `attendance` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `employee_id` int(11) NOT NULL,
  `date` date NOT NULL,
  `clock_in_time` time NOT NULL,
  `clock_out_time` time NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

In this table, we store information related to each punch-in, including employee ID, date, work time and off work time, etc.

  1. Create PHP background file
    We use PHP to handle background logic and read data from the database. First we need to create a file named "getAttendance.php" and write the following code:
<?php

// 连接到数据库
$conn = new mysqli("localhost", "username", "password", "attendance");

// 检查连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 查询数据库中的考勤记录
$sql = "SELECT * FROM attendance";
$result = $conn->query($sql);

// 检查查询结果是否为空
if ($result->num_rows > 0) {
    // 将查询结果转换为JSON格式,并输出给前端
    $rows = array();
    while ($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    echo json_encode($rows);
} else {
    echo "没有找到考勤记录";
}

// 关闭数据库连接
$conn->close();
Copy after login

In this file, we first connect to the database, and then query the attendance records in the database, And convert the result into JSON format and output it to the front end.

  1. Create Vue front-end code
    Next we need to create a Vue front-end page to display attendance records. First create a file named "index.html" and introduce the Vue and axios libraries. Then write the following code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>员工考勤报告</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
    <div id="app">
        <table>
            <thead>
                <tr>
                    <th>员工ID</th>
                    <th>日期</th>
                    <th>上班时间</th>
                    <th>下班时间</th>
                </tr>
            </thead>
            <tbody v-if="attendance.length">
                <tr v-for="record in attendance" :key="record.id">
                    <td>{{ record.employee_id }}</td>
                    <td>{{ record.date }}</td>
                    <td>{{ record.clock_in_time }}</td>
                    <td>{{ record.clock_out_time }}</td>
                </tr>
            </tbody>
            <tbody v-else>
                <tr>
                    <td colspan="4">没有找到考勤记录</td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        new Vue({
            el: '#app',
            data: {
                attendance: []
            },
            mounted() {
                this.getAttendance();
            },
            methods: {
                getAttendance() {
                    axios.get('getAttendance.php')
                        .then(response => {
                            this.attendance = response.data;
                        })
                        .catch(error => {
                            console.log(error);
                        });
                }
            }
        });
    </script>
</body>
</html>
Copy after login

In this code, we create a Vue instance and call the getAttendance method in the mounted hook function to obtain the attendance record. Then use the v-for directive to generate each row of the table.

  1. Run the project
    Now we only need to place the above two files in the root directory of the server and access the "index.html" file in the browser to see the generated Online employee attendance reports.

Through the above steps, we successfully used PHP and Vue to generate an online employee attendance report. Of course, this is just a simple example and you can make more complex developments based on actual needs. Hope this article is helpful to you!

The above is the detailed content of How to generate online employee attendance report via PHP and Vue. For more information, please follow other related articles on the PHP Chinese website!

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