All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Dynamic Pagination In PHP With MySQL Example

Last Updated : Mar 11, 2024

Dynamic Pagination In PHP With MySQL Example

In this tutorial we will show you the solution of dynamic pagination in PHP with MySQL example, Pagination is the process with help of which we are able to display data in multiple pages rather them showing them on a single page.

With help of pagination we divide the specific number of records in multiple pages.

This is mostly used when there is a lot data to display. The best example of pagination is Google when you search something on Google, pagination is present at bottom of that each page.

Step By Step Guide On Dynamic Pagination In PHP With MySQL Example :-

Here, below we are going to create a pagination that is dynamic in nature.

In last article, we understand that how to create pagination with next and previous button, this article is same like that but the concept is here that pagination should be dynamic.

It means that when we click on a particular page number then the data related to that page number must be appear there. So, let us see how to create a dynamic pagination code in php with MySQL.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
 Dynamic pagination in php with mysql example
 </title>
    <script src="js/jquery.js"></script>
</head>
<body>
<?php
    $connect= mysqli_connect("localhost", "root", "", "as1");
?>
<table border="2px solid black" width="100%">
    <tr>
        <th>
            id
        </th>
        <th>
            sname
        </th>
        <th>
            marks_one
        </th>
        <th>
            marks_two
        </th>
        <th>
            marks_three
        </th>
        <th>
            marks_four
        </th>
    </tr>
    <?php
        $limit = 5;
        $pi="1";
        if (empty($_REQUEST['p'])) {
            $start=0;
        }
        else
        {
            $pi=$_REQUEST['p'];
            $end = $pi * $limit;
            $start = $end - $limit;
        }
        $query = " select * from student_marks limit $start,$limit";
        $result = mysqli_query($connect,$query);
        while ($row=mysqli_fetch_assoc($result)) {
            ?> <tr>
            <td><?php echo $row['id'] ?></td>
            <td><?php echo $row['sname'] ?></td>
            <td><?php echo $row['marks_one'] ?></td>
            <td><?php echo $row['marks_two'] ?></td>
            <td><?php echo $row['marks_three'] ?></td>
            <td><?php echo $row['marks_four'] ?></td>
            </tr>
            <?php
        }
    ?>
    <tr>
        <td colspan="6" style="height: 35px;">
            <?php
                $query = " select * from student_marks";
                $result = mysqli_query($connect,$query);
                $count = mysqli_num_rows($result);
                // echo $count;
                $pages= ceil($count/$limit);
                // echo $pages;
                for ($i=1; $i <= $pages; $i++) {
                    ?>
                        <a style="margin: 10px;text-decoration:none;" href="pageination.php?p=<?php echo $i ?>">
                            <?php
                            if ($i==$pi) {
                                ?>
                                    <span style="background-color:black; text-align:center; color:white; padding:5px;margin:5px;">
                                        <?php
                                              echo $i;
                                        ?>
                                    </span>
                                <?php
                            }
                            else {
                                # code...
                                echo $i;
                            }
                            ?>
                        </a>
                    <?php
                }
            ?>
        </td>
    </tr>
</table>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as you can see that we create database connection to get data from database using MySQL. In next some steps we get data from our required table and to show data we create a table with some heading and then our pagination codes runs.
  6. Here, as you see that we create a limit of 5 it means that only 5 rows are showed on particular page. And then as you see we use ceil() here. This is because if total records found are 17, for example. Then there must be 4 pages. On first three page the rows are 5 and at last there are only 2 rows.
  7. To show page number we use for loop also. Now, we hope that you understand how to create dynamic pagination in php with MySQL.
  8. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to create dynamic pagination in php with MySQL Database.

I hope this tutorial on dynamic pagination in PHP with MySQL example helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪