PHP Pagination
This article will show you a way to perform paging in PHP using the LIMIT clause and the SQL
INSERT SELECT
statement in PHP. We will use LIMIT
the LIMIT clause to select a specific number of rows starting from a specific index to display as paging.
Previous
We will also demonstrate another method of adding and navigation functionality in PHP paging Next
. This method simply adds the additional functionality of navigating the corresponding page to the first method.
In PHP SQL
, use LIMIT
the clause and SELECT
the statement to page rows
We can use LIMIT
the clause and SELECT
the statement to specify the first n results to be displayed on the page. We can anchor
provide the page number as GET
a method in the tag to navigate through the pages. 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 $_GET
the array and isset()
the function to get the page number requested by the user.
For example, create a variable $results_per_page
and store it in it 2
. Use mysqli_num_rows()
the function to find the number of rows in the database and store it in $number_of_results
the variable. Use ceil()
the function to determine the total number of pages required to display the rows. Divide $number_of_results
the variable by the variable ceil()
inside the function $results_per_page
. Use $_GET
the superglobal variable and isset()
the function to check page
if the variable is set. If it is not set, $page
set the variable to 1
. If the variable is set, assign the value to $page
the variable. 1
Subtract $page
the variable from and multiply it by $this_page_first_result
the variable. Store the operation in the variable $this_page_first_result
. Write an LIMIT
if statement with a while SQL
clause to SELECT * FROM alpha LIMIT ' . $this_page_first_result . ',' . $results_per_page.
run the query and display the results. Finally, create a for
loop to loop between $page
the variables and $number_of_page
. Inside the loop, echo
anchor
tags and href
set the value of the property to index.php?page'.$page
. anchor
Write $page
the variable between the tags.
In the example below, alpha
the table in the database contains six rows. After paging is performed, two rows are displayed per page. ceil()
The function determines the total number of pages required to display the rows. Initially, $page
the variables are not set, so the pages start at page 1. $this_page_first_result
The variables determine the page number the user is currently on. The variable $this_page_first_result
represents the first row of the page. The variable $results_per_page
represents the number of results per page. The output section displays index.php
pages. When page is clicked 2
, 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']. '<br>';
}
for ($page=1;$page<=$number_of_pages;$page++) {
echo '<a href="index.php?page=' . $page . '">' . $page . '</a> ';
}
?>
Output:
1 A
2 B
1 2 3
Previous
Adding and Next
navigation functions to PHP paging
Previous
We can add some additional code snippets to the code example of the first method to provide and navigation functionality in pagination Next
. We can $page
increment and decrement the variable by 1 to point to the previous and next pages. We can use anchor
the increment and decrement variables in the tags to implement the Next
and Previous
functionality.
For example, create two variables, $prev
and $next
. Subtract $page
the variable from 1 and assign the operation to $prev
the variable. Similarly, add 1 to $page
the variable and assign it to the variable. Echo a tag before the loop $next
for the page number that says . Assign the attribute of the tag to . In the same way, create another tag for using the variable as the value of in the attribute .for
anchor
Previous
anchor
href
index.php?page=' . $prev .
$next
href
page
Next
anchor
In the output section below, it shows the second page. Click Previous
to go to the first page and click Next
to go to the third page.
Sample code:
# php 7.x
<?php
$prev = $page -1;
$next = $page +1;
echo ' <a href="index.php?page=' . $prev . '"> Previous </a> ';
for ($page=1;$page<=$number_of_pages;$page++) {
echo '<a href="index.php?page=' . $page . '">' . $page . '</a> ';
}
echo ' <a href="index.php?page=' . $next . '"> Next </a> ';
?>
Output:
3 C
4 D
Previous 1 2 3 Next
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of