Home >Backend Development >PHP Tutorial >How to use PHP and Vue to build a data visualization interface for employee attendance

How to use PHP and Vue to build a data visualization interface for employee attendance

WBOY
WBOYOriginal
2023-09-26 13:49:51777browse

How to use PHP and Vue to build a data visualization interface for employee attendance

How to use PHP and Vue to build a data visualization interface for employee attendance

Introduction:
In modern enterprise management, employee attendance is a very important link. In order to improve management efficiency and monitor employee attendance, many companies have adopted automated attendance systems. This article will use PHP and Vue framework to build a data visualization interface for employee attendance to help companies better monitor and analyze employee attendance data.

1. Data collection and storage
First, we need to collect employee attendance data and store it in the database. Attendance data includes employee names, clock-in time, attendance status and other information.

In PHP, you can use the MySQL database for storage. First, we need to create a database named "attendance", and then create a table named "employee" with fields: id, name, time and status.

The following is the corresponding code example:

// 连接数据库
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "attendance";

$conn = new mysqli($servername, $username, $password, $dbname);

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

// 创建员工表格
$sql = "CREATE TABLE employee (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(30) NOT NULL,
    time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    status ENUM('迟到', '早退', '请假', '正常') NOT NULL
)";

if ($conn->query($sql) === TRUE) {
    echo "表格已创建成功";
} else {
    echo "创建表格失败: " . $conn->error;
}

$conn->close();

2. Build a data visualization interface
Next, we use the Vue framework to build a data visualization interface so that companies can visually view employees attendance status.

  1. Front-end part
    In the front-end part, we use Vue components to define the data visualization interface for employee attendance. First, we need to introduce the Vue and Axios (used for sending HTTP requests) libraries and create a Vue instance.

The following is the corresponding code example:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>员工考勤数据可视化</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
    <div id="app">
        <h1>员工考勤数据可视化</h1>
        <table>
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>打卡时间</th>
                    <th>考勤状态</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="employee in employees" :key="employee.id">
                    <td>{{ employee.name }}</td>
                    <td>{{ employee.time }}</td>
                    <td>{{ employee.status }}</td>
                </tr>
            </tbody>
        </table>
    </div>

    <script>
        new Vue({
            el: '#app',
            data: {
                employees: []
            },
            mounted() {
                this.fetchEmployees();
            },
            methods: {
                fetchEmployees() {
                    axios.get('api.php')
                        .then(response => {
                            this.employees = response.data;
                        })
                        .catch(error => {
                            console.log(error);
                        });
                }
            }
        });
    </script>
</body>
</html>
  1. Backend part
    In the backend part, we need to use PHP to write an API for downloading data from the database Obtain employee attendance data and return it to the front end.

The following is the corresponding code example:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "attendance";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

$sql = "SELECT * FROM employee";
$result = $conn->query($sql);

$employees = array();

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $employees[] = $row;
    }
}

echo json_encode($employees);

$conn->close();
?>

3. Result display
Through the above steps, we successfully built a data visualization interface for employee attendance. When accessing the page, the front end sends an HTTP request to obtain the employee attendance data returned by the back end and displays it in a table.

Through this interface, enterprise managers can intuitively understand the attendance status of each employee, promptly discover and handle attendance abnormalities, and improve management efficiency.

Conclusion:
By using a combination of PHP and Vue, we implemented a simple employee attendance data visualization interface. Of course, this is just a basic example, and you can expand and optimize it according to the actual situation to meet your own needs.

I hope this article will be helpful to you in building a visual interface for employee attendance data!

The above is the detailed content of How to use PHP and Vue to build a data visualization interface for employee attendance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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