Php

Simple tutorial of pagination in php with Demo

In this PHP tutorial, we’ll learn how to make pagination with PHP and MySql. The pagination can be ajax-based, but in this case, I’m creating pagination without ajax. To create dynamic pagination, I’m using bootstrap3 and PHP5. I’m assuming you’ve got a table and some data.

Normally, SQL SELECT query may return results containing thousands or millions of records. displaying all of those records on a single page is not a good idea. So, the output can be divided into multiple pages.

What is Pagination?

Paging allows you to display all of your retrieved results on multiple pages rather than all of them on a single page.

I am following the simple steps outlined below:

  • Step 1: I will connect a MySQL database with a table.
  • Step 2: Data is retrieved from a MySQL table and displayed in the table.
  • Step 3: I’ll add a pagination link at the bottom of the table.

Checkout other tutorials on pagination,

Related Post

Let’s make an index.php file and paste the code below into it. This code will assist you in creating pagination in PHP. To create pagination at the bottom of the table, I’ll use the bootstrap pagination component.

$dbhost = 'localhost';  
$dbuser = 'root';  
$dbpass = '';  
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');  
$dbname = 'test';
$conn = mysqli_connect($dbhost, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
    /* check connection */    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }    
$limit = 2;  
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };  
$start_from = ($page-1) * $limit;  
  
$sql = "SELECT * FROM posts ORDER BY title ASC LIMIT $start_from, $limit";  
$rs_result = mysqli_query($conn, $sql);    
};  

Let’s create a table and iterate rows data on table row using a while loop.

jQuery('.getvalue').on('click', function(e) { var selValue = document.querySelector('input[name = "radioGrp"]:checked').value; alert(selValue); });
<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>title</th>
            <th>body</th>
        </tr>
    </thead>
    <thead></thead>
    <tbody>
        <?php while ($row = mysql_fetch_assoc($rs_result)) {?>
        <tr>
            <td><?php echo $row["title"]; ?></td>
            <td><?php echo $row["body"]; ?></td>
        </tr>
       <?php } ?>
    </tbody>
</table>

Now, we will count all records and create pagination –

$sql = "SELECT COUNT(id) FROM posts";  
$rs_result = mysqli_query($conn, $sql);  
$row = mysql_fetch_row($rs_result);  
$total_records = $row[0];  
$total_pages = ceil($total_records / $limit);  
$pagLink = '

<div class="pagination">'; for ($i=1; $i<=$total_pages; $i++) { $pagLink .= <a href="index.php?page=">".$i."</a> };</div>
<div class="pagination">echo $pagLink . ""; ?></div>

Watch on Youtube

Result:

You can check live demo, YouTube video and download source code

Tags: pagination

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories