PHP paging plays an important role in website development, which can help page content be displayed in pages and improve user experience. PHP editor Xigua will introduce how to use PHP to implement the paging function, making the website easier to browse and manage. By studying this article, readers can easily master the principles and implementation methods of PHP paging, and add new skills and inspiration to website development.
We will also demonstrate another way to addPrevious
andNext
navigation functionality in PHP pagination. This method only adds the additional functionality of navigating the corresponding page to the first method.
LIMIT
clause and theSELECT
statement inSQL
We can use theLIMIT
clause and theSELECT
statement to specify the first n results to be displayed on the page. We can browse the page by providing the page number in theanchor
tag as theGET
method. In this method, we define the number of rows to be displayed per page and retrieve all rows from thedatabaseto calculate the total number of pages required. We can use the$_GET
arrayandisset()
functions to get the page number requested by the user.
For example, create a variable$results_per_page
and store2
in it. Use theMysqli_num_rows()
function to find the number of rows in thedatabaseand store it in the$number_of_results
variable. Use theceil()
function to determine the total number of pages required to display rows. Divide the$number_of_results
variable by the$results_per_page
variable inside theceil()
function. Use the$_GET
superglobal variable and theisset()
function to check if thepage
variable is set. Set the$page
variable to1
if it is not already set. If the variable is set, the value is assigned to the$page
variable. Subtract the$page
variable from1
and multiply it by the$this_page_first_result
variable. Store the operation in the variable$this_page_first_result
. Write aSQL
statement with aLIMIT
clause asSELECT * FROM alpha LIMIT ' . $this_page_first_result . ',' . $results_per_page.
Run the query and display result. Finally, create afor
loop to loop between the$page
variable and the$number_of_page
. Inside the loop,echo
anchor
tag and set the value of thehref
attribute toindex.php?page'.$page
. Write the$page
variable between theanchor
tags.
In the example below, thealpha
table in the database contains six rows. After performing paging, two lines are displayed per page.ceil()
Function determines the total number of pages required to display rows. Initially, the$page
variable is not set, so the page starts on page 1.$this_page_first_result
Variable determines the page number the user is currently on. The variable$this_page_first_result
represents the first line of the page. The variable$results_per_page
represents the number of results per page. The output section displays theindex.php
page. When page2
is clicked, it outputs the next two rows from the database.
Sample code:
#php 7.x php $number_of_pages = ceil($number_of_results/$results_per_page); if (!isset($_GET['page'])) { $page = 1; } else { $page = $_GET['page']; } $this_page_first_result = ($page-1)*$results_per_page; $sql='SELECT * FROM alpha LIMIT ' . $this_page_first_result . ',' . $results_per_page; $result = mysqli_query($con, $sql); while($row = mysqli_fetch_array($result)) { echo $row['id'] . ' ' . $row['value']. '
'; } for ($page=1;$page<=$number_of_pages;$page++) { echo '. $page . '">' . $page . ' '; } ?>
Output:
1 A 2 B 1 2 3
Previous
andNext
navigation in PHP pagination FeaturesWe can add some additional code snippets to the code example of the first approach to providePrevious
andNext
navigation functionality in pagination. We can increment and decrement the$page
variable by 1 to point to the previous and next pages. We can use the increment and decrement variables in theanchor
tag to implement theNext
andPrevious
functions.
For example, create two variables,$prev
and$next
. Subtract the$page
variable by 1 and assign the action to the$prev
variable. Similarly, add 1 to the$page
variable and assign it to the$next
variable. Echoes aanchor
tag that saysPrevious
before the page number'sfor
loop. Assign thehref
attribute of theanchor
tag toindex.php?page=' . $prev .
. In the same way, create anotheranchor for
Nextusing the
$nextvariable as the value of
pagein the
hrefattribute
tag.
In the output section below, it shows the second page. ClickPrevious
to enter the first page, clickNext
to enter the third page.
Sample code:
# php 7.x php $prev = $page -1; $next = $page +1; echo ' . $prev . '"> Previous '; for ($page=1;$page<=$number_of_pages;$page++) { echo '. $page . '">' . $page . ' '; } echo ' . $next . '"> Next '; ?>
Output:
3 C 4 D Previous 1 2 3 Next
The above is the detailed content of PHP pagination. For more information, please follow other related articles on the PHP Chinese website!