Simple PHP Pagination Example Tutorial

Simple PHP Pagination Example Tutorial

Today, We want to share with you Simple PHP Pagination Example Tutorial.In this post we will show you Creating Paging using PHP and MySQL, hear for dynamic pagination in php with mysql example we will give you demo and example for implement.In this post, we will learn about pagination code in php with next and previous with an example.

Simple PHP Pagination Example Tutorial

There are the Following The simple About Simple PHP Pagination Example Tutorial Full Information With Example and source code.

As I will cover this Post with live Working example to develop Simple pagination for array collections, so the some Simple PHP Pagination script for this example is following below.

Read Also:  call to undefined function mysql_pconnect()

PHP array pagination Example

<!DOCTYPE html> 
<html> 
<head> 
    <title>Simple PHP Pagination script</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <style> 
    .inline{ 
        display: inline-block; 
        float: right; 
        margin: 20px 0px; 
    } 
    
    input, button{ 
        height: 34px; 
    } 
    </style> 
</head> 
<body> 
<?php 
    require_once "db_connection.php"; 

    $limit = 10; 
    if (isset($_GET["page"])) { 
    $page_num = $_GET["page"]; 
    } 
    else { 
    $page_num=1; 
    }; 

    $start_from = ($page_num-1) * $limit; 

    $sql = "SELECT * FROM products LIMIT $start_from, $limit"; 
    $products_res = mysql_query ($sql); 

?> 
<div class="container"> 
    <br> 
    <div> 
    <h1>Dynamic pagination in php with mysql example</h1> 
    <p>This page is just for Simple PHP Examples of Basic Pagination using PHP.</p> 
    <table class="table table-striped table-condensed table-bordered"> 
        <thead> 
        <tr> 
        <th width="10%">Rank</th> 
        <th>Name</th> 
        <th>College</th> 
        <th>Score</th> 
        </tr> 
        </thead> 
        <tbody> 
        <?php 
        while ($get_row = mysql_fetch_array($products_res, MYSQL_ASSOC)) { 
        ?> 
        <tr> 
        <td><?php echo $get_row["rank"]; ?></td> 
        <td><?php echo $get_row["name"]; ?></td> 
        <td><?php echo $get_row["college"]; ?></td> 
        <td><?php echo $get_row["score"]; ?></td>                                        
        </tr> 
        <?php 
        }; 
        ?> 
        </tbody> 
    </table> 
    <div> 
    <ul class="pagination"> 
    <?php 
        $sql = "SELECT COUNT(*) FROM products"; 
        $products_res = mysql_query($sql); 
        $get_row = mysql_fetch_get_row($products_res); 
        $total_records = $get_row[0]; 
        $product_total_pages = ceil($total_records / $limit); 
        $k = (($page_num+4>$product_total_pages)?$product_total_pages-4:(($page_num-4<1)?5:$page_num));      
        $pagLink = ""; 
        if($page_num>=2){ 
            echo "<li><a href='index.php?page=1'> << </a></li>"; 
            echo "<li><a href='index.php?page=".($page_num-1)."'> < </a></li>"; 
        } 
        for ($i=-4; $i<=4; $i++) { 
        if($k+$i==$page_num) 
            $pagLink .= "<li class='active'><a href='index.php?page=".($k+$i)."'>".($k+$i)."</a></li>"; 
        else
            $pagLink .= "<li><a href='index.php?page=".($k+$i)."'>".($k+$i)."</a></li>"; 
        }; 
        echo $pagLink; 
        if($page_num<$product_total_pages){ 
            echo "<li><a href='index.php?page=".($page_num+1)."'> > </a></li>"; 
            echo "<li><a href='index.php?page=".$product_total_pages."'> >> </a></li>"; 
        }    
    ?> 
    </ul> 
    <div class="inline"> 
    <input id="page_num" type="number" min="1" max="<?php echo $product_total_pages?>"
    placeholder="<?php echo $page_num."/".$product_total_pages; ?>" required> 
    <button onclick="next_page_redirect();">Go</button> 
    </div> 
    </div> 
    </div> 
</div> 
<script> 
    function next_page_redirect() 
    { 
        var page_num = document.getElementById("page_num").value; 
        page_num = ((page_num><?php echo $product_total_pages; ?>)?<?php echo $product_total_pages; ?>:((page_num<1)?1:page_num)); 
        window.location.href = 'index.php?page='+page_num; 
    } 
</script> 
</body> 
</html> 

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  Bootstrap Toggle switch with ajax update to mysql in PHP

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Simple PHP Pagination Example Tutorial.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *