As companies grow, managing employees and monitoring attendance become essential tasks. To improve work efficiency and reduce labor costs, many enterprises choose to use technology to manage attendance data. This article will guide you on how to develop a practical employee attendance management panel by combining PHP and Vue.
Before starting the attendance management system, ensure the following environments and tools are ready:
Create a MySQL database named attendance and design the following two tables:
Employee table employees:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
department VARCHAR(255) NOT NULL
);
Attendance records table attendance_records:
CREATE TABLE attendance_records (
id INT AUTO_INCREMENT PRIMARY KEY,
employee_id INT NOT NULL,
date DATE NOT NULL,
time_in TIME NOT NULL,
time_out TIME,
FOREIGN KEY (employee_id) REFERENCES employees(id)
);
Use PHP's mysqli extension to connect to MySQL. Example code:
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "attendance";
<p>$conn = new mysqli($servername, $username, $password, $dbname);</p>
<p>if ($conn->connect_error) {<br>
die("Database connection failed: " . $conn->connect_error);<br>
}<br>
?><br>
Create API endpoints to handle CRUD operations for employees and attendance records. Below is an example to fetch the employee list:
<?php
// Get employee list
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
<p>if ($result->num_rows > 0) {<br>
$employees = array();<br>
while ($row = $result->fetch_assoc()) {<br>
$employees[] = $row;<br>
}<br>
// Return JSON format data<br>
echo json_encode($employees);<br>
} else {<br>
echo "No employees found";<br>
}<br>
?><br>
Similar endpoints can be created for managing attendance records.
Run the following command in the terminal to initialize a new Vue project:
vue create attendance-management-panel
Choose the default configuration when prompted.
Use Vue Router to set up routes and views to display employee lists and attendance records.
Use the axios library to call backend APIs and display employee data. Example code:
import axios from 'axios';
<p>export default {<br>
data() {<br>
return {<br>
employees: []<br>
}<br>
},<br>
mounted() {<br>
axios.get('/api/employees')<br>
.then(response => {<br>
this.employees = response.data;<br>
})<br>
.catch(error => {<br>
console.log(error);<br>
});<br>
}<br>
}<br>
More components can be created as needed to implement full attendance record management features.
Upload the built frontend code to the server's web directory and deploy the backend PHP code. Make sure the server is properly configured. Access the system through a browser to test all attendance management functions.
This article introduced how to build a basic employee attendance management panel using PHP and Vue, including database design, backend API development, and frontend page construction with sample code. For real projects, you can extend and optimize features according to specific needs. Hopefully, this guide helps you create an efficient and practical attendance system.