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.
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;
In this table, we store information related to each punch-in, including employee ID, date, work time and off work time, etc.
<?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();
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.
<!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>
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.
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!