1. Problem When there is a lot of data in the database, you should query only a part of it each time to relieve the pressure on the server and the page. Here we use the Pagination component of The following figure is the most basic paging style: Of course, corresponding events need to be introduced to query the database when the page changes. 2. Solution2.1 Paging Component<el-pagination background layout="prev, pager, next" :page-size="8" :total="total" :current-page="pageNum" @current-change="handleCurrentChange"> </el-pagination>
2.2 Function to get database data: getData(): The parameters are getData(offset,limit){ this.axios.post('/php/select.php', qs.stringify({ offset: offset, limit: limit, type: 'Lost and Found' }), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then((res) => { if(res.data === 0){ this.total = 0; this.list = []; return; } this.total = res.data.total this.list = res.data.data this.loading = false }).catch((err) => { this.$message.error(err) }) } 2.3 The page is loaded and the data of the first page needs to be requestedcreated () { this.getData(0,8); }, The page change triggers the Call getData to query data on different pages: handleCurrentChange(val){ this.list = [] // Clear the previous page data this.getData((val-1)*8,8); } Below is the backend data: Now there are 10 records in the data table: The select.php: <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "Database name"; // Create a connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $type = $_POST['type']; //Get the start and end number of the front-end parameters if ( !isset( $_POST['offset'] ) ) { echo 0; exit(); }; $offset = ( int )$_POST['offset']; if ( !isset( $_POST['limit'] ) ) { echo 0; exit(); }; $limit = ( int )$_POST['limit']; //Pagination query database $sql = "SELECT * FROM posts where type='$type' order by id desc LIMIT $limit OFFSET $offset"; $result = $conn->query($sql); $sqlGetCount = "SELECT COUNT(*) cnt FROM posts where type='$type'"; $rescnt = $conn->query($sqlGetCount); $rescnt = $rescnt->fetch_assoc(); $arr = array(); if ($result->num_rows > 0) { while ( $row = $result->fetch_assoc() ) { array_push( $arr, $row ); } //echo json_encode( $arr, JSON_UNESCAPED_UNICODE ); echo json_encode(array_merge(array('data'=>$arr),array('total'=>(int)$rescnt['cnt']))); } else { echo 0; } mysqli_close( $conn ); ?> Here, SQL statement: "SELECT * FROM posts where type='$type' order by id desc LIMIT $limit OFFSET $offset" 3. Analysis Here, For example, $limit = 8, $offest = 0: means querying the first 8 data in the database, starting from 0 (not including 0, MySQL index starts from 0), querying 8 data, that is, 1 to 8 data. At this time, the parameter At the same time, select.php returns the total number of data entries: SELECT COUNT(*) cnt FROM posts where type='$type' After the front-end page obtains the 4. Results Note: Your This is the end of this article about Vue+ElementUI implementing paging query-mysql data. For more relevant Vue+ElementUI implementing paging query content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Specific use of MySQL window functions
>>: The cloud server uses Baota to build a Python environment and run the Django program
How to define complex components (class component...
Introduction The Docker-Compose project is an off...
This article uses examples to illustrate the func...
background Use idea with docker to realize the wh...
We, humble coders, still have to sing, "You ...
systemd: The service systemctl script of CentOS 7...
After going through a lot of hardships, I searched...
This blog post is about a difficulty encountered ...
1. Use curl command to access by default: # curl ...
Method 1: Adding values Let's go to MDN to se...
<template> <div id="root"> ...
Table of contents 1. Resource download 2. Unzip t...
1. Find a suitable version of redis for docker Yo...
Table of contents Rendering API changes Render fu...
Table of contents introduction Cookie What are Co...