PHP pagination

WBOY
Release: 2024-02-28 09:14:01
forward
926 people have browsed it

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 addPreviousandNextnavigation functionality in PHP pagination. This method only adds the additional functionality of navigating the corresponding page to the first method.


Page rows in PHP using theLIMITclause and theSELECTstatement inSQL

We can use theLIMITclause and theSELECTstatement to specify the first n results to be displayed on the page. We can browse the page by providing the page number in theanchortag as theGETmethod. 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$_GETarrayandisset()functions to get the page number requested by the user.

For example, create a variable$results_per_pageand store2in it. Use theMysqli_num_rows()function to find the number of rows in thedatabaseand store it in the$number_of_resultsvariable. Use theceil()function to determine the total number of pages required to display rows. Divide the$number_of_resultsvariable by the$results_per_pagevariable inside theceil()function. Use the$_GETsuperglobal variable and theisset()function to check if thepagevariable is set. Set the$pagevariable to1if it is not already set. If the variable is set, the value is assigned to the$pagevariable. Subtract the$pagevariable from1and multiply it by the$this_page_first_resultvariable. Store the operation in the variable$this_page_first_result. Write aSQLstatement with aLIMITclause asSELECT * FROM alpha LIMIT ' . $this_page_first_result . ',' . $results_per_page.Run the query and display result. Finally, create aforloop to loop between the$pagevariable and the$number_of_page. Inside the loop,echoanchortag and set the value of thehrefattribute toindex.php?page'.$page. Write the$pagevariable between theanchortags.

In the example below, thealphatable 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$pagevariable is not set, so the page starts on page 1.$this_page_first_resultVariable determines the page number the user is currently on. The variable$this_page_first_resultrepresents the first line of the page. The variable$results_per_pagerepresents the number of results per page. The output section displays theindex.phppage. When page2is 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 . ' ';
} ?>
Copy after login

Output:

1 A 2 B 1 2 3 
Copy after login

AddPreviousandNextnavigation in PHP pagination Features

We can add some additional code snippets to the code example of the first approach to providePreviousandNextnavigation functionality in pagination. We can increment and decrement the$pagevariable by 1 to point to the previous and next pages. We can use the increment and decrement variables in theanchortag to implement theNextandPreviousfunctions.

For example, create two variables,$prevand$next. Subtract the$pagevariable by 1 and assign the action to the$prevvariable. Similarly, add 1 to the$pagevariable and assign it to the$nextvariable. Echoes aanchortag that saysPreviousbefore the page number'sforloop. Assign thehrefattribute of theanchortag toindex.php?page=' . $prev .. In the same way, create anotheranchor forNextusing the$nextvariable as the value ofpagein thehrefattributetag.

In the output section below, it shows the second page. ClickPreviousto enter the first page, clickNextto 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  '; ?> 
Copy after login

Output:

3 C 4 D Previous 1 2 3 Next 
Copy after login

The above is the detailed content of PHP pagination. For more information, please follow other related articles on the PHP Chinese website!

source:lsjlt.com
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
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!