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
Table of contents tool: Login scenario: practice:...
This article example shares the specific code of ...
I've been playing with the remote development...
Triggers can cause other SQL code to run before o...
//Default protocol /The use of the default protoc...
Nginx can generally be used for seven-layer load ...
Table of contents background Problem Description ...
Table of contents 1 Version and planning 1.1 Vers...
Table of contents Install and introduce axios dep...
I recently installed Ubuntu 20.04 and found that ...
Jupyter notebook is configured under the docker c...
As a super rookie, I just started learning MySQL ...
Azure Container Registry is a managed, dedicated ...
Today is still a case of Watch app design. I love...
Every time I design a web page or a form, I am tr...