All TalkersCode Topics

Follow TalkersCode On Social Media

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

Pagination Code In PHP With Next And Previous

Last Updated : Mar 11, 2024

Pagination Code In PHP With Next And Previous

In this tutorial we will show you the solution of pagination code in PHP with next and previous, 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 Pagination Code In PHP With Next And Previous :-

Here, to create pagination in php. There must be created a database and a table with multiple values inside that table.

In this article, our database name is as1 and table name is student_marks in which there is a column for student id, student name, marks one, marks two, marks three and marks four.

Now, let us see how to use this data in code.

<!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>Document</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 "<ul class='pagination'>";
    echo "<li><a href='pagination.php?pi=".($pi-1)."' class='button'>Previous</a></li>";
                // 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
                }
    echo "<li><a href='pagination.php?pi=".($pi+1)."' class='button'>NEXT</a></li>";
    echo "</ul>";
            ?>
        </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 see that we first create a database connection first. Then we create a table to show headings of data. Then we fetch data from database and show it in table.
  6. Now, we set a limit that the data the number of column showed are 5. And when we click on previous or next button then previous and next page showed.
  7. At last, when our button click value is equal to number of page then that data showed. We hope that you understand this article properly.
  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 pagination code with next and previous button in php.

I hope this tutorial on pagination code in PHP with next and previous helps you and the steps and method mentioned above are easy to follow and implement.