How to use PHP and Vue to design data display for employee attendance system

WBOY
Release: 2023-09-25 18:28:01
Original
1148 people have browsed it

How to use PHP and Vue to design data display for employee attendance system

How to use PHP and Vue to design the data display of employee attendance system

Introduction:
In modern enterprises, the attendance system is a very important part and can be effective Manage employee attendance effectively and improve work efficiency. With the help of PHP and Vue, we can design a simple and efficient employee attendance system, and intuitively display the employee's attendance status through the data display function. This article will introduce how to use PHP and Vue to design the data display of the employee attendance system, and provide specific code examples.

1. Build the environment
To use PHP and Vue to design the data display of the employee attendance system, first we need to build the corresponding environment. Please follow the steps below:

  1. Install PHP
    You can download the latest version of PHP from the PHP official website (https://www.php.net/downloads.php) and install it according to guide for installation. After the installation is complete, you can enter php -v in the terminal or command prompt to check whether PHP is installed successfully.
  2. Installing Vue CLI
    Vue CLI is a scaffolding tool used to quickly build Vue projects. You can use npm (Node.js package manager) to install it. Enter the following command in a terminal or command prompt to install the Vue CLI:

    npm install -g @vue/cli
    Copy after login
  3. Create a Vue project
    In the command prompt, switch to the directory where you wish to create the project, And enter the following command to create a new Vue project:

    vue create employee-attendance-system
    Copy after login
  4. Create PHP script
    In the root directory of your new Vue project, create a PHP script file named api.php . This file will be used to process requests sent from the Vue front end and return corresponding data.

2. Write the front-end code
Next, we will write the Vue front-end code to display employee attendance data. Please follow the steps below:

  1. Modify App.vue
    Open the App.vue file in the src directory and modify it as follows:

    <template>
      <div>
        <h1>员工考勤系统</h1>
        <table>
          <thead>
            <tr>
              <th>员工姓名</th>
              <th>日期</th>
              <th>上班时间</th>
              <th>下班时间</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="record in records" :key="record.id">
              <td>{{ record.name }}</td>
              <td>{{ record.date }}</td>
              <td>{{ record.startTime }}</td>
              <td>{{ record.endTime }}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          records: [],
        };
      },
      mounted() {
        this.fetchRecords();
      },
      methods: {
        fetchRecords() {
          // 使用axios发送GET请求到后端的api.php脚本
          axios.get('api.php')
            .then(response => {
              this.records = response.data;
            })
            .catch(error => {
              console.error(error);
            });
        },
      },
    };
    </script>
    Copy after login
  2. Modify main.js
    Open the main.js file in the src directory and modify it as follows:

    import Vue from 'vue';
    import App from './App.vue';
    
    Vue.config.productionTip = false;
    
    new Vue({
      render: h => h(App),
    }).$mount('#app');
    Copy after login

3. Write the back-end code
Now, we will write the back-end PHP code to handle the requests sent from the Vue front-end and return the corresponding data. Please follow the steps below:

  1. Write api.php
    Open the api.php file in the root directory and modify it as follows:

    <?php
    
    // 模拟员工考勤数据
    $records = [
      [
        'id' => 1,
        'name' => '员工1',
        'date' => '2020-01-01',
        'startTime' => '09:00:00',
        'endTime' => '18:00:00',
      ],
      [
        'id' => 2,
        'name' => '员工2',
        'date' => '2020-01-02',
        'startTime' => '08:30:00',
        'endTime' => '17:30:00',
      ],
    ];
    
    // 设置响应头,允许跨域访问
    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');
    
    // 将员工考勤数据以JSON格式返回给前端
    echo json_encode($records);
    Copy after login
  2. Run the front-end and back-end code
    In the command prompt, switch to the root directory of your project and enter the following command to start the front-end and back-end code:

    npm run serve
    Copy after login

    This will start A local development server and listening on http://localhost:8080. You can access this address in your browser to see the data display page of the employee attendance system.

Conclusion:
Through the above steps, we successfully designed a simple and efficient employee attendance system using PHP and Vue, and implemented the data display function. Of course, this is just a basic example and you can extend and improve the code according to your own needs. I hope this article is helpful to you, and I wish you success in designing data display for employee attendance systems using PHP and Vue!

Note: The above code examples are for demonstration purposes only. There may be some security or performance issues. Please make appropriate improvements and optimizations according to your needs in actual applications.

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

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!