PHP MySQL Google Like Pagination With Source Code & Database

10-08-2017

PHP MySQL Google Like Pagination With Source Code & Database

Fetching and Listing Database Table Records in Page is Easy in PHP Mysql. But, When We have Huge Records in Database Table, We Fetch Records and Paginate it into Pagination. It is Very Simple and a Little bit Tricky.


Pagination is very Useful when we list a large number of records. We will show the records page-wise so that loading large records will not effect our page load speed. Here in the below example, I'm giving demo of how to create Goole Like pagination in PHP MySQL.

Create DataBase and Table

In my case, I'm Creating Database name as 'pagination' and Table name as 'names'. In 'names' Table, Store the names of your 20-30 friends and we are going to show all names using pagination. Open your SQL Terminal and Write the following command to create Database and Table

CREATE DATABASE pagination

CREATE TABLE names(
id INT(10) NOT NULL AUTO_INCREMENT,
name VARCHAR(200) NOT NULL ,
PRIMARY KEY (id)
);

Pagination Code Sample Using PHP & MySQL

	<?php 
	//connection to DB
	$connection = new MySQLi('localhost','root','','pagination');
	// Set per page records to display
	$num_record_per_page=2; 
	//set current page
	if( isset($_GET["page"]) ){
	$page = $_GET["page"]; 
	}else{
	$page=1;
	}
	//Count Total Records
	$sql_count_rows="SELECT COUNT(1) FROM names";
	$res_rows=$connection->query($sql_count_rows);
	$total_rows =$res_rows->fetch_array()[0];
	//Count Total Pages
	$total_pages=ceil($total_rows/$num_record_per_page);
	//Set start from number for sql query
	$start_from = ($page-1)*$num_record_per_page;
	//SQL fetch query according to page
	$sql="SELECT *FROM names LIMIT $start_from,$num_record_per_page";
	$rows=$connection->query($sql);
	while ($result=$rows->fetch_assoc()) {
	echo $result["name"];
	echo "<br>";
	}
	?>

	<style type="text/css">
	ul li{display: inline;}
	</style>
	<ul>
	  <?php if ($page > 1): ?>
	  <li><a href="index.php?page=<?php echo $page-1 ?>"> << </a></li>
	  <?php endif; ?>

	  <?php if ($page > 3): ?>
	  <li ><a href="index.php?page=1">1</a></li>
	  <li class="dots">...</li>
	  <?php endif; ?>

	  <?php if ($page-2 > 0): ?><li ><a   href="index.php?page=<?php echo $page-2 ?>"><?php echo $page-2 ?></a></li><?php endif; ?>

	  <?php if ($page-1 > 0): ?><li ><a   href="index.php?page=<?php echo $page-1 ?>"><?php echo $page-1 ?></a></li><?php endif; ?>

	  <li style="background: #4ad295"><a href="index.php?page=<?php echo $page ?>"><?php echo $page ?></a></li>

	  <?php if ($page+1 < ceil($total_rows /$num_record_per_page)+1): ?><li ><a href="index.php?page=<?php echo $page+1 ?>"><?php echo $page+1 ?></a></li><?php endif; ?>

	  <?php if ($page+2 < ceil($total_rows /$num_record_per_page)+1): ?><li ><a href="<?php  echo $_pgcurrent_page; ?>?page=<?php echo $page+2 ?>"><?php echo $page+2 ?></a></li><?php endif; ?>

	  <?php if ($page < ceil($total_rows / $num_record_per_page)-2): ?>
	  <li >...</li>
	  <li ><a   href="index.php?page=<?php echo ceil($total_rows /$num_record_per_page) ?>"><?php echo ceil($total_rows /$num_record_per_page) ?></a></li>
	  <?php endif; ?>


	  <?php if ($page < ceil($total_rows / $num_record_per_page)): ?>
	  <li  aria-label="Next"><a   href="index.php?page=<?php echo $page+1 ?>">  >></a></li>
	  <?php endif; ?>
	</ul>


Here is a Working Copy of the Download Link With the Database and PHP Source Code.
Download Zip File With Source Code & Database


Latest Published Blog


PurgeCSS - Remove unused CSS from Web Pages

PurgeCSS - Remove unused CSS from Web Pages

03-04-2023

It is generally a good practice to remove unused CSS from web pages. As mentioned earlier, removing unused CSS can provide several benefits, including faster page loading times, improved website performance, reduced page weight, and better code maintainability.


Installing Asterisk16 on CentOs7

Installing Asterisk16 on CentOs7

14-04-2022

Here is Step-by-Step Guide to Installing Asterisk16 on CentOs7.


Make CentOS7 Full Screen Like Primary Operating System in VirtualBox

Make CentOS7 Full Screen Like Primary Operating System in VirtualBox

01-03-2022

Learn How to Make CentOS7 Full Screen Like Primary Operating System in VirtualBox. After Installing CentOS in VirtualBox, CentOs Screen does not Occupy the Full Window Screen. Here in this Tutorial, We Will See how to do it.


Setting  Key to a Column For Quick Fetch Operation From Larze Records

Setting Key to a Column For Quick Fetch Operation From Larze Records

14-02-2019

To set a key to a column in SQL, you need to create an index on that column. An index is a data structure that allows the database to quickly look up records based on the values in the indexed column. By creating an index on a column, you can improve the performance of queries that filter, sort, or group by that column.