Home >Backend Development >PHP Tutorial >How to develop an online employee attendance system using PHP and Vue
How to use PHP and Vue to develop an online employee attendance system
The attendance system is one of the important tools for enterprise management. It can help enterprises monitor the attendance of employees in real time. Improve work efficiency and management level. This article will introduce how to use PHP and Vue framework to develop a simple online employee attendance system and provide specific code examples.
1. Environment preparation:
Before you start, you need to install the following software and tools:
2. Create database tables:
In order to store employee information and attendance records, we need to create two database tables: employee table and attendance record table.
3. Back-end development:
Get all employee information:
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
}
echo json_encode($rows);
$sql = "INSERT INTO employees (name, department) VALUES ('$name', '$department')";
if ($conn->query($sql) === TRUE) {
echo "New employee added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Get attendance records:
$sql = "SELECT attendance.* , employees.name FROM attendance INNER JOIN employees ON attendance.employee_id = employees.id";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
}
echo json_encode($rows );
$sql = "INSERT INTO attendance (employee_id, clock_in, clock_out) VALUES ('$employee_id', '$clock_in', '$clock_out ')";
if ($conn->query($sql) === TRUE) {
echo "Clock in/out recorded successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
四, Front-end development:
Create a Vue component to display the employee list, add employees and clock in records.
Employee list
{{ employee.name }} - {{ employee.department }}
Add employee
Punch-in record
staff
Working hours
Off-duty time
{{ record.name }}
{{ record.clock_in }}
{{ record.clock_out }}
在Vue组件中,发送HTTP请求并获取数据:
<script><br>export default {<br> data() {<br> return {<br> employees: [],<br> newEmployee: {},<br> records: []<br> };<br> },<br> mounted() {<br> this.getEmployees();<br> this.getRecords();<br> },<br> methods: {<br> getEmployees() {<br> fetch('attendance.php?type=getEmployees')<br> .then(response => response.json())<br> .then(data => {</script>
this.employees = data;
});
},
addEmployee() {
fetch('attendance.php?type=addEmployee', {
method: 'POST', body: JSON.stringify(this.newEmployee)
})
.then(response => response.text())
.then(data => {
alert(data); this.getEmployees();
});
},
getRecords() {
fetch('attendance.php?type=getRecords')
.then(response => response.json())
.then(data => {
this.records = data;
});
},
clockIn() {
// 发送打卡请求
},
clockOut() {
// 发送打卡请求
}
}
}
以上是使用PHP和Vue框架开发在线员工考勤系统的基本步骤和代码示例。希望本文能够为你提供参考和指导,祝你开发成功!
The above is the detailed content of How to develop an online employee attendance system using PHP and Vue. For more information, please follow other related articles on the PHP Chinese website!