Categories: Php

Simple Pagination with PHP and MySQl Using jQuery

Pagination is useful to show large data records into chunks of data,In previous article we have learn simple pagination non Ajax using PHP.I got huge response from readers and some request as well.This tutorial about how to make simple pagination using php MySQL with jquery simplePagination.js plugin.

Main problem was that display about millions of records,That creates issues when the number of pages are thousands.We have use advanced type pagination in php with jquery simplepagination plugin.Simplepagination.js is a simple jQuery pagination plugin,which are supporting CSS3 themes and Bootstrap support.

Checkout other tutorials of pagination,

Simple Pagination with PHP and Mysql

Step 1: included all js and css files.

Related Post
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="dist/simplePagination.css" />
<script src="dist/jquery.simplePagination.js"></script>

Step 2: Created connection with MySQL using PHP.

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "test";

    $conn = mysqli_connect($servername, $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();
    } 
    

Step 3: Created HTML table with MySQL records.

    <table class="table table-bordered">  
        <thead>  
        <tr>  
        <th>Name</th>  
        <th>Salary</th>
        <th>Age</th>  
        </tr>  
        </thead>  
        <tbody>  
        <?php  
        while ($row = mysqli_fetch_assoc($rs_result)) {
        ?>  
                    <tr>  
                    <td><?php echo $row["employee_name"]; ?></td>  
                    <td><?php echo $row["employee_salary"]; ?></td>  
                    <td><?php echo $row["employee_age"]; ?></td>  
                    </tr>  
        <?php  
        };  
        ?>  
        </tbody>  
        </table>
          
        <?php  
        $sql = "SELECT COUNT(id) FROM employee";  
        $rs_result = mysqli_query($conn, $sql);  
        $row = mysqli_fetch_row($rs_result);  
        $total_records = $row[0];  
        $total_pages = ceil($total_records / $limit);  
        $pagLink = "<nav><ul class='pagination'>";  
        for ($i=1; $i<=$total_pages; $i++) {  
                     $pagLink .= "<li><a href='index.php?page=".$i."'>".$i."</a></li>";  
        };  
        echo $pagLink . "</ul></nav>";  
        ?>

Step 4: Call simplePagination methods on pagination container.

        <script type="text/javascript">
            $(document).ready(function(){
            $('.pagination').pagination({
                    items: <?php echo $total_records;?>,
                    itemsOnPage: <?php echo $limit;?>,
                    cssStyle: 'light-theme',
                    currentPage : <?php echo $page;?>,
                    hrefTextPrefix : 'index.php?page='
                });
                });
            </script>
    

You can download source code and Demo from below link.

View Comments

Recent Posts

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

2 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

2 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

3 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

3 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

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories