AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Simple pagination script PHP MySQLi


Free Scripts » Simple pagination script PHP and MySQLi

 

However, in this article, we will use HTML, CSS PHP and MySQLi to create pagination.

Steps to Create Simple Pagination Using PHP and MySQLi

  1. Create a Database and Table with Dummy Data
  2. Create a Database Connection
  3. Get the Current Page Number
  4. SET Total Records Per Page Value
  5. Calculate OFFSET Value and SET other Variables
  6. Get the Total Number of Pages for Pagination
  7. SQL Query for Fetching Limited Records using LIMIT Clause and OFFSET
  8. Showing Current Page Number Out of Total
  9. Creating Pagination Buttons

 

simple pagination script PHP MySQLy

 

Demo and download the script.
Demo Pagination script Download Pagination script

 

Note: use external CSS.

 

HTML and CSS code pagination

Creating Structure: In this section, we will just create the basic website structure of the pagination.

CREATE DATABASE;

 1. create a table

CREATE TABLE IF NOT EXISTS `pagination_table` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
`age` int(10) NOT NULL,
`dept` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. Create a Database Connection

$con = mysqli_connect("localhost","root","","agernic_pagination");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}

3. Get the Current Page Number

To get the current page number, we will use the $_GET .

if (isset($_GET['page_no']) && $_GET['page_no']!="") {
$page_no = $_GET['page_no'];
} else {
$page_no = 1;
}

 

simple pagination script code

4. SET Total Records Per Page Value

$total_records_per_page = 10;

 

5. Calculate OFFSET Value and SET other Variables

$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";

 

6. Get the Total Number of Pages for Pagination

$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

 

7. SQL Query for Fetching Limited Records using LIMIT Clause and OFFSET

$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);

 

create table rows

<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>
<!--
All your PHP Script will be here
-->
</tbody>
</table>

 

Simple pagination HTML

8. Showing Current Page Number Out of Total

<div style='padding: 10px 20px 0px; border-top: dotted 1px #CCC;'>
<strong>Page <?php echo $page_no." of ".$total_no_of_pages; ?></strong>
</div>

 

9. Creating Pagination Buttons

<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>

if ($total_no_of_pages <= 10){
for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
if ($counter == $page_no) {
echo "<li class='active'><a>$counter</a></li>";
}else{
echo "<li><a href='?page_no=$counter'>$counter</a></li>";
}
}
}

Get demo and download the script.
Demo Pagination Download Pagination script

 



add simple pagination in html table
Simple pagination script PHP MySQLi - free scripts

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...