Creating dynamic next/previous buttons with PHP and MySQL.
Tags: php, mysql, programming
Published: 2020-12-17 17:14:35

Users can generate this functionality with the help of pagination. Pagination means simple fetching and displaying data into multiple pages rather than displaying the whole data on a single page. We can create the pagination by following steps;

---
title: Creating dynamic next/previous buttons with PHP and MySQL.
tags: mysql, php
canonical_url: https://kodlogs.com/blog/243/creating-dynamic-next-previous-buttons-with-php-and-mysql
---

Create a database table.
Create a database connection.
**Get the current page number:**
To get the current page we will use the get method;
```
if (isset($_GET['page_no']) && $_GET['page_no']!="") {

    $page_no = $_GET['page_no'];

    } else {

        $page_no = 1;

        }
```
**Set the total amount of records per page:**
```
$total_records_per_page = 3;

$offset = ($page_no-1) * $total_records_per_page;

$previous_page = $page_no - 1;

$next_page = $page_no + 1;

$adjacents = "2";
```
**Get the total number of pages for pagination:**
It will depend upon how many records you want to display on a single page. The code for this as follows;
```
$result_count = mysqli_query(

$con,

"SELECT COUNT(*) As total_records FROM `pagination_table`"

);

$total_records = mysqli_fetch_array($result_count);

$total_records = $total_records['total_records'];

$total_no_of_pages = ceil($total_records / $total_records_per_page);

$second_last = $total_no_of_pages - 1; // total pages minus 1
```
 

**Apply SQL query for fetching the records:**
For this purpose we will use OFFSET and total records per page;

```
       $result = mysqli_query( $con,"SELECT * FROM `pagination_table` LIMIT $offset,$total_records_per_page"  );

while($row = mysqli_fetch_array($result)){

    echo "<tr>

 <td>".$row['id']."</td>

 <td>".$row['name']."</td>

 <td>".$row['age']."</td>

 <td>".$row['dept']."</td>

 </tr>";

        }

mysqli_close($con);
```
This code will only display the table rows. Users must have to ensure that the table header has been added. The table header will be added by the following code.
```
<table class="table table-striped table-bordered">

<thead>

<tr>

<th style='width:50px;'>ID</th>

<th style='width:150px;'>Name</th>

<th style='width:50px;'>Age</th>

<th style='width:150px;'>Department</th>

</tr>

</thead>

<tbody>
//body of the data will be here

</tbody>

</table>
```
Show the current page number out of total:
To display the current page number use the following code;

```
<div style='padding: 10px 20px 0px; border-top: dotted 1px #CCC;'>

<strong>Page <?php echo $page_no." of ".$total_no_of_pages; ?></strong>

</div>

Create the pagination button:

<ul class="pagination">

<?php if($page_no > 1){

echo "<li><a href='?page_no=1'>First Page</a></li>";

} ?>

<li <?php if($page_no <= 1){ echo "class='disabled'"; } ?>>

<a <?php if($page_no > 1){

echo "href='?page_no=$previous_page'";

} ?>>Previous</a>

</li>  

<li <?php if($page_no >= $total_no_of_pages){

echo "class='disabled'";

} ?>>

<a <?php if($page_no < $total_no_of_pages) {

echo "href='?page_no=$next_page'";

} ?>>Next</a>

</li>


<?php if($page_no < $total_no_of_pages){

echo "<li><a href='?page_no=$total_no_of_pages'>Last &rsaquo;&rsaquo;</a></li>";

} ?>

</ul>
```
The above code will create very beautiful pagination buttons.