JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

PHP Pagination

Author:JIYIK Last Updated:2025/04/12 Views:

This article will show you a way to perform paging in PHP using the LIMIT clause and the SQLINSERT SELECTstatement in PHP. We will use LIMITthe LIMIT clause to select a specific number of rows starting from a specific index to display as paging.

PreviousWe 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 LIMITthe clause and SELECTthe statement to page rows

We can use LIMITthe clause and SELECTthe statement to specify the first n results to be displayed on the page. We can anchorprovide the page number as GETa 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 $_GETthe array and isset()the function to get the page number requested by the user.

For example, create a variable $results_per_pageand 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_resultsthe variable. Use ceil()the function to determine the total number of pages required to display the rows. Divide $number_of_resultsthe variable by the variable ceil()inside the function $results_per_page. Use $_GETthe superglobal variable and isset()the function to check pageif the variable is set. If it is not set, $pageset the variable to 1. If the variable is set, assign the value to $pagethe variable. 1Subtract $pagethe variable from and multiply it by $this_page_first_resultthe variable. Store the operation in the variable $this_page_first_result. Write an LIMITif statement with a while SQLclause to SELECT * FROM alpha LIMIT ' . $this_page_first_result . ',' . $results_per_page.run the query and display the results. Finally, create a forloop to loop between $pagethe variables and $number_of_page. Inside the loop, echo anchortags and hrefset the value of the property to index.php?page'.$page. anchorWrite $pagethe variable between the tags.

In the example below, alphathe 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, $pagethe variables are not set, so the pages start at page 1. $this_page_first_resultThe variables determine the page number the user is currently on. The variable $this_page_first_resultrepresents the first row of the page. The variable $results_per_pagerepresents the number of results per page. The output section displays index.phppages. 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

PreviousAdding and Nextnavigation functions to PHP paging

PreviousWe can add some additional code snippets to the code example of the first method to provide and navigation functionality in pagination Next. We can $pageincrement and decrement the variable by 1 to point to the previous and next pages. We can use anchorthe increment and decrement variables in the tags to implement the Nextand Previousfunctionality.

For example, create two variables, $prevand $next. Subtract $pagethe variable from 1 and assign the operation to $prevthe variable. Similarly, add 1 to $pagethe variable and assign it to the variable. Echo a tag before the loop $nextfor 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 .foranchorPreviousanchorhrefindex.php?page=' . $prev .$nexthrefpageNextanchor

In the output section below, it shows the second page. Click Previousto go to the first page and click Nextto 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.

Article URL:

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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial