Home  >  Article  >  Backend Development  >  PHP pagination

PHP pagination

WBOY
WBOYforward
2024-02-28 09:13:31992browse

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 add Prev<strong class="keylink">io</strong>us and Next navigation 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 the LIMIT clause and the SELECT statement in SQL

We can use the LIMIT clause and the SELECT statement to specify the first n results to be displayed on the page. We can browse the page by providing the page number in the anchor tag as the GET method. In this method, we define the number of rows to be displayed per page and retrieve all rows from the database to calculate the total number of pages required. We can use the $_GET array and isset() functions to get the page number requested by the user.

For example, create a variable $results_per_page and store 2 in it. Use the <strong class="keylink">Mysql</strong>i_num_rows() function to find the number of rows in the database and store it in the $number_of_results variable. Use the ceil() 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 the ceil() function. Use the $_GET superglobal variable and the isset() function to check if the page variable is set. Set the $page variable to 1 if it is not already set. If the variable is set, the value is assigned to the $page variable. Subtract the $page variable from 1 and multiply it by the $this_page_first_result variable. Store the operation in the variable $this_page_first_result. Write a SQL statement with a LIMIT clause as SELECT * FROM alpha LIMIT ' . $this_page_first_result . ',' . $results_per_page. Run the query and display result. Finally, create a for loop to loop between the $page variable and the $number_of_page. Inside the loop, echo anchor tag and set the value of the href attribute to index.php?page'.$page. Write the $page variable between the anchor tags.

In the example below, the alpha 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 the index.php page. When page 2 is clicked, it outputs the next two rows from the database.

Sample code:

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#408080;font-style:italic">#php 7.x
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#19177c">$number_of_pages</span> <span style="color:#666">=</span> ceil(<span style="color:#19177c">$number_of_results</span><span style="color:#666">/</span><span style="color:#19177c">$results_per_page</span>);
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">if</span> (<span style="color:#666">!</span>isset(<span style="color:#19177c">$_GET</span>[<span style="color:#ba2121">'page'</span>])) {
</span></span><span style="display:flex;"><span><span style="color:#19177c">$page</span> <span style="color:#666">=</span> <span style="color:#666">1</span>;
</span></span><span style="display:flex;"><span>} <span style="color:#008000;font-weight:bold">else</span> {
</span></span><span style="display:flex;"><span><span style="color:#19177c">$page</span> <span style="color:#666">=</span> <span style="color:#19177c">$_GET</span>[<span style="color:#ba2121">'page'</span>];
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#19177c">$this_page_first_result</span> <span style="color:#666">=</span> (<span style="color:#19177c">$page</span><span style="color:#666">-</span><span style="color:#666">1</span>)<span style="color:#666">*</span><span style="color:#19177c">$results_per_page</span>;
</span></span><span style="display:flex;"><span><span style="color:#19177c">$sql</span><span style="color:#666">=</span><span style="color:#ba2121">'SELEC&#84; * FROM alpha LIMIT '</span> <span style="color:#666">.</span> <span style="color:#19177c">$this_page_first_result</span> <span style="color:#666">.</span> <span style="color:#ba2121">','</span> <span style="color:#666">.</span> <span style="color:#19177c">$results_per_page</span>;
</span></span><span style="display:flex;"><span><span style="color:#19177c">$result</span> <span style="color:#666">=</span> <strong class="keylink">mysql</strong>i_query(<span style="color:#19177c">$con</span>, <span style="color:#19177c">$sql</span>);
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">while</span>(<span style="color:#19177c">$row</span> <span style="color:#666">=</span> mysqli_fetch_array(<span style="color:#19177c">$result</span>)) {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#19177c">$row</span>[<span style="color:#ba2121">'id'</span>] <span style="color:#666">.</span> <span style="color:#ba2121">' '</span> <span style="color:#666">.</span> <span style="color:#19177c">$row</span>[<span style="color:#ba2121">'value'</span>]<span style="color:#666">.</span> <span style="color:#ba2121">'<br>'</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">for</span> (<span style="color:#19177c">$page</span><span style="color:#666">=</span><span style="color:#666">1</span>;<span style="color:#19177c">$page</span><span style="color:#666"><=</span><span style="color:#19177c">$number_of_pages</span>;<span style="color:#19177c">$page</span><span style="color:#666">++</span>) {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">'<a href="index.php?page='</span> <span style="color:#666">.</span> <span style="color:#19177c">$page</span> <span style="color:#666">.</span> <span style="color:#ba2121">'">'</span> <span style="color:#666">.</span> <span style="color:#19177c">$page</span> <span style="color:#666">.</span> <span style="color:#ba2121">'</a> '</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>

Output:

<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>1 A 
</span></span><span style="display:flex;"><span>2 B 
</span></span><span style="display:flex;"><span>1 2 3
</span></span></code></code>

Add Previous and Next navigation in PHP pagination Features

We can add some additional code snippets to the code example of the first approach to provide Previous and Next 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 the anchor tag to implement the Next and Previous 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 a anchor tag that says Previous before the page number's for loop. Assign the href attribute of the anchor tag to index.php?page=' . $prev .. In the same way, create another anchor for Next using the $next variable as the value of page in the href attribute tag.

In the output section below, it shows the second page. Click Previous to enter the first page, click Next to enter the third page.

Sample code:

<code><code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#408080;font-style:italic"># php 7.x
</span></span></span><span style="display:flex;"><span><span style="color:#408080;font-style:italic"></span><span style="color:#666"><?</span>php
</span></span><span style="display:flex;"><span><span style="color:#19177c">$prev</span> <span style="color:#666">=</span> <span style="color:#19177c">$page</span> <span style="color:#666">-</span><span style="color:#666">1</span>;
</span></span><span style="display:flex;"><span><span style="color:#19177c">$next</span> <span style="color:#666">=</span> <span style="color:#19177c">$page</span> <span style="color:#666">+</span><span style="color:#666">1</span>;
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">' <a href="index.php?page='</span> <span style="color:#666">.</span> <span style="color:#19177c">$prev</span> <span style="color:#666">.</span> <span style="color:#ba2121">'"> Previous </a> '</span>;
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">for</span> (<span style="color:#19177c">$page</span><span style="color:#666">=</span><span style="color:#666">1</span>;<span style="color:#19177c">$page</span><span style="color:#666"><=</span><span style="color:#19177c">$number_of_pages</span>;<span style="color:#19177c">$page</span><span style="color:#666">++</span>) {
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">'<a href="index.php?page='</span> <span style="color:#666">.</span> <span style="color:#19177c">$page</span> <span style="color:#666">.</span> <span style="color:#ba2121">'">'</span> <span style="color:#666">.</span> <span style="color:#19177c">$page</span> <span style="color:#666">.</span> <span style="color:#ba2121">'</a> '</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">echo</span> <span style="color:#ba2121">' <a href="index.php?page='</span> <span style="color:#666">.</span> <span style="color:#19177c">$next</span> <span style="color:#666">.</span> <span style="color:#ba2121">'"> Next </a> '</span>;
</span></span><span style="display:flex;"><span><span style="color:#bc7a00">?></span><span >
</span></span></span></code></code>

Output:

<code><code class="language-text hljs" data-lang="text"><span style="display:flex;"><span>3 C 
</span></span><span style="display:flex;"><span>4 D 
</span></span><span style="display:flex;"><span>Previous 1 2 3 Next
</span></span></code></code>

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

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete