Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of data pagination display by PHP + MySQL

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example of PHP + MySQL to achieve data paging display, the article is very detailed, has a certain reference value, interested friends must read it!

What are the characteristics of php 1, the execution speed is fast. 2. It has good openness and expansibility. 3. PHP supports a variety of mainstream and non-mainstream databases. Object-oriented programming: PHP provides classes and objects. 5. The update speed of the version is fast. 6. It has rich functions. 7. Scalability. 8. Comprehensive functions, including graphics processing, encoding and decoding, compressed file processing, xml analysis and so on.

First, connect to the database $connect = mysqli_connect ('localhost',' username', 'password', 'database name') or die ('database connection failed'); mysqli_set_charset ($connect, 'utf8'); second, build the SQL statement

Paging is actually implemented by using the limit keyword in MySQL. For example, we query the first two pieces of data in a table:

Select * from student limit 0,2

After limit, you need two parameters, the first parameter represents which article to start with, and the second parameter represents how many items to take at a time.

Then the meaning of the above SQL statement is to start with Article 0 and take 2 (in fact, 0 is Article 1, and the count starts from 0).

We take the first two pieces of data as the first page.

Then continue to take the second page, or 2 pieces of data, then the SQL statement is:

Select * from student limit 2,2

Page 3:

Select * from student limit 4, 2

By analogy, we find that each page is always two, so the second parameter (that is, 2) of limit has been determined, so how to determine the first parameter? In fact, the rules are as follows:

Page 1: 0J2 (page 1 is taken from the position of 0, take 2, that is, 0 1)

Page 2: 2jue 2 (page 2 is taken from the position of 2, take 2, that is, 2 3)

Page 3: 4J2 (page 3 takes 2 articles from the position of 4, that is, 4 5)

Page 4: 6J2 (page 4 is taken from the position of 6, take 2, that is, 6 7)

……

So the starting position of each page = (current page-1) * the number of entries displayed per page

Page 1: (1-1) * 2 = 0

Page 2: (2-1) * 2 = 2

Page 3: (3-1) * 2 = 4

……

Third, define the number of entries displayed per page $pageSize = 2; fourth, define the current page $page = 1; fifth, calculate the first parameter needed by the limit according to the formula $start = ($page-1) * $pageSize

So the SQL statement is:

Select * from student limit $start, $pageSize

When you change the value of $page, you can query the data of the corresponding page.

6. Get the $page parameter by passing parameters in the address bar to define the current page.

In order to be more flexible, we pass parameters through the address bar to get the number of pages we want to display, so the code for step 4 is changed to:

If ($_ GET ['page']) {$page = $_ GET [' page'];} else {$page = 1; / / cannot be processed according to 1, that is, page 1} 7. Control the switch between the upper and lower pages through the button

Execute the SQL statement:

$sql = "select * from student limit $start, $pageSize"; $query = mysqli_query ($connect, $sql); $result = mysqli_fetch_all ($query, MYSQLI_ASSOC)

Export the results to the HTML table:

Student list ID name, age, gender, mobile phone number

Add upper and lower buttons:

Previous page the next page

When clicking on the previous page, we need to give the current page $page-1

When we click on the next page, we need to give the current page $page + 1

Determine the previous page based on the current page:

$up = $page-1

Assuming that the current page is page 2, $up is 2-1 = 1, that is, the previous page is page 1.

Determine the next page based on the current page:

$next = $page + 1

Assuming that the current page is page 2, $next is 2 + 1 = 3, that is, the next page is page 3.

So we add a hyperlink to the button, link to the current page, and take a page parameter with the corresponding number of pages.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report