How to Make Pagination in Php With Source Code

0

Hey Guys, Hope You All of fine, in this tutorial, We Will Learn How to Make Pagination in Php with MySQL Video Tutorial.  Do you want to develop any web application using PHP and MySQL, you need to make a Pagination.  Basically, Users use Pagination to move to another page easily.

If you visited more than one website, after completing the home page, you can see the Number of Pages Available at the bottom. It is called Pagination, So, Do you want to make Pagination in PHP with MySQL Database, and I will teach you how you can do that?

Pagination in PHP

Furthermore, More than PHP Developers design the Pagination in PHP using different ways. If you are a PHP developer or learner, you know everyone developers use different tactics to complete any task. I am also a Programmer, So, I will teach easy and simple methods, you can use it to Make Pagination in PHP.

How to Create Pagination Links in Php

inside the video tutorial, you can learn How to Make Pagination in Php with MySQLi Database, I will also share with you the complete source code of this project. You can also download the source code below.

Dynamic Pagination in Php with MySQL Example

Lets’ Get Started to Make Pagination in PHP, First of All, you need to Create Database, and tables and Insert the Manual Records in the Database Tables. In my case I have made the Database “PHP Tutorials” My Table Name is “Employees”. Below I have shared the Screen short, you can see it and then follow it.

Create-Database-Tables-and-Insert-the-Records

After Creating the Database, Table, Also Insert the Records in the Tables. You need to, Make a Connection Between PHP and MySQL.

///Connection.PHP File Name 
<?php 
	mysql_connect('localhost','root',''); 
	mysql_select_db('phptutorials'); 
?>

Then you need to display the Database Records in the Browser. So, I have made the complete codes, I have shared the below, you can see it.

<?php 

	include_once('connection.php');

	$num_per_page=05;


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

	$start_from=($page-1)*05;

	$sql="select * from employees limit $start_from,$num_per_page";
	$rs_result=mysql_query($sql);


?>

<!DOCTYP html>
<html>
    <head>
        <title> Pagination in PHP</title>
    </head>
<body>
    
    <table align="center" border="1px">
        <tr>
            <th> Employee ID </th>
            <th> Employee Name </th>
            <th> Employee Email </th>
            <th> Employee Salary </th>
        </tr>
        
        <?php 
        
        while($rows=mysql_fetch_array($rs_result))
        {
        ?>
        
        <tr>
            <td><?php echo $rows['Employee_ID'];?></td>
            <td><?php echo $rows['Name'];?></td>
            <td><?php echo $rows['Email'];?></td>
            <td><?php echo $rows['Salary'];?></td>
        </tr>  
        
        <?php     
        }
                
        ?>    
    </table>
    
    <?php 
    
    
    $sql="select * from employees";
    $rs_result=mysql_query($sql);
    $total_records=mysql_num_rows($rs_result);
    $total_pages=ceil($total_records/$num_per_page);
    
    for($i=1;$i<=$total_pages;$i++)
    {
        echo "<a href='index.php?page=".$i."'>".$i."</a>" ;
    }
    
    ?>
    

    
</body>
</html>

Now, After completing the above process, then you can see the Pagination in the Browsers. In these codes, you can see the Number Five, I want to display the record Per Page 05. Do you want to increase, just need to change the number that’s it.

Pagination Code in PHP with Next and Previous

before moving on to the codes, you need to watch a complete tutorial that helps you to make pagination in PHP with next and previous buttons. Once you learned that, then you can get the source code of the project below. I’ve shared each code that’s used inside the video tutorial So, so Let’s get started.

You May Also Like:

First of All, you need to make a config.php file inside the file, you need to make a connection between Php and MySQLi. I’ve shared the simple shortcode below, you can check it out now.

<?php 

    $con = mysqli_connect('localhost','root','','sr_system');
    if(!$con)
    {
        echo ' Please Check Your Connection ';
    }


?>

Once you use the above-mentioned code, you need to change one thing namely the Database name, you can change the database which you made inside the local computer and replace the database name on this code.

Then, You need to make an index.php file inside the file, you need to make simple code that helps us to check the get variable is active then assign the value otherwise assign the value 0.

<?php 

    require_once('config.php');

    if(isset($_GET['page']))
    {
        $page = $_GET['page'];
    }
    else
    {
        $page = 1;
    }

    $num_per_page = 02;
    $start_from = ($page-1)*02;
    
    $query = "select * from pagination limit $start_from,$num_per_page";
    $result = mysqli_query($con,$query);

?>

Inside the code you need to get the connection file and use the condition that helps us to check the Get Variable is Active, then create $page variable and assign the Get Variable value. Once the Check the get Variable value, then you need to create another variable namely $num_per_page, and assign the values. In My case I’ve assigned 02 values, once you assign the 02 values in $num_per_page then inside the page you can see two records will appear on the page.

However, the next you need to make variable $start_from = ($page-1)*2 once you made $start_from variable and assign the page minus-1 means 0 values multiplication by 2. Two values have to assign $num_per_page, You can change the $num_per_page value then you need to change the formula which appeared $start_from variable.

After that, you need to write the simple query that helps us to display the database record, but you need to also include two things such as $start_from and also $num_per_page; start from a variable that helps us to display the starting number values and num per page variable that help to display the number of record inside page.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="bootstrap.css">
    <title>Pagination in Php With Next and Previous</title>
</head>
<body>
        <table class="table table-striped">
            <tr>
                <td> User ID </td>
                <td> User Name </td>
                <td> User Email </td>
            </tr>
            <tr>
                <?php 
                    while($row=mysqli_fetch_assoc($result))
                    {
                ?>
                    <td> <?php echo $row['ID'] ?> </td>
                    <td> <?php echo $row['UserName'] ?> </td>
                    <td> <?php echo $row['Email'] ?> </td>
                   
            </tr>
         <?php 
                }
                ?>
        </table>

        <?php 
        
                $pr_query = "select * from pagination ";
                $pr_result = mysqli_query($con,$pr_query);
                $total_record = mysqli_num_rows($pr_result );
                
                $total_page = ceil($total_record/$num_per_page);

                if($page>1)
                {
                    echo "<a href='index.php?page=".($page-1)."' class='btn btn-danger'>Previous</a>";
                }

                
                for($i=1;$i<$total_page;$i++)
                {
                    echo "<a href='index.php?page=".$i."' class='btn btn-primary'>$i</a>";
                }

                if($i>$page)
                {
                    echo "<a href='index.php?page=".($page+1)."' class='btn btn-danger'>Next</a>";
                }
        
        ?>

        
</body>
</html>

Inside the code, you can get the values from the database and assign the values inside the HTML table, and also you can use another query to display the pagination in php with the next and previous buttons.

I hope you are understood as well all about pagination in php. I also include the source code for this project, you can download that on your personal computer and then check out each code that’s used inside the video tutorial.

So, I have shared the codes and Also Video Tutorial. If you face any problems, kindly comment to me in the below comment section, after receiving your comment, I will contact you very soon. However, If you like my tutorial, kindly share the content on Social Networks and Subscribe Your Email To Getting Updates.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.