pagination in core php

Step1  create  database connection and get data from database and then apply given code
Step2 : 

<html>
<head>
<title></title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div style="width:700px; margin:0 auto;">

<h3> Bootstrap Pagination Using CorePHP and MySQL</h3>
<table class="table table-striped table-bordered">
	<thead>
		<tr>
			<th>ID</th>
			<th>Title</th>
			<th >Description</th>
			<th>Status</th>
		</tr>
	</thead>

<tbody>

<?php
/* Create connnection in php */
include('db.php');


/* Set Page No for Pagination*/
if (isset($_GET['page_no']) && $_GET['page_no']!="") 
 {
	$page_no = $_GET['page_no'];
 }
 else
 {
	$page_no = 1;
 }

	$total_records_per_page = 2; /*==how many recoreds per page want to display====*/

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


	$previous_page = $page_no - 1;
	
	$next_page = $page_no + 1;
	
	$adjacents = "1"; 

	$result_count = mysqli_query($connection,"SELECT COUNT(*) As total_records FROM gm_services");
	$total_records = mysqli_fetch_array($result_count);
	$total_records = $total_records['total_records'];

	
    $total_pages = ceil($total_records / $total_records_per_page);
	$second_last = $total_pages - 1; 

    $result = mysqli_query($connection,"SELECT * FROM  gm_services LIMIT $offset, $total_records_per_page");
    while($row = mysqli_fetch_array($result))
    {
		echo "<tr>
			  <td>".$row['id']."</td>
			  <td>".$row['title']."</td>
	 		  <td>".substr($row['description'],10). "</td>
		   	  <td>".$row['status']."</td>
		   	  </tr>";
    }

?>
</tbody>
</table>
<!--  Pagination  Page 1 of 12  -->
<div style='padding: 10px 20px 0px;'>

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

<ul class="pagination">
	
	<li <?php if($page_no <= 1){ echo "class='disabled'"; } ?>>
	<a <?php if($page_no > 1){ echo "href='?page_no=$previous_page'"; } ?>>Previous</a>
	</li>
       
    <?php 
	if ($total_pages <= 10)
	{  	 
		for ($count = 1; $count <= $total_pages; $count++)
		{
			if ($count == $page_no) 
			{
		   		echo "<li class='active'><a>$count</a></li>";	
			}
			else
			{
          		echo "<li><a href='?page_no=$count'>$count</a></li>";
			}
        }
	}
	elseif($total_pages > 10)
	{
		
		if($page_no <= 4) 
		{			
	 		for ($count = 1; $count < 8; $count++)
	 		{		 
				if ($count == $page_no) 
				{
		  			 echo "<li class='active'><a>$count</a></li>";	
				}
				else
				{
           			echo "<li><a href='?page_no=$count'>$count</a></li>";
				}
        	}

			echo "<li><a>...</a></li>";
			echo "<li><a href='?page_no=$second_last'>$second_last</a></li>";
			echo "<li><a href='?page_no=$total_pages'>$total_pages</a></li>";
		}

	elseif($page_no > 4 && $page_no < $total_pages - 4)
	{		 
		echo "<li><a href='?page_no=1'>1</a></li>";
		echo "<li><a href='?page_no=2'>2</a></li>";
        echo "<li><a>...</a></li>";

        for($count = $page_no - $adjacents; $count <= $page_no + $adjacents; $count++) 
        {			
           if ($count == $page_no) 
           {
		   		echo "<li class='active'><a>$count</a></li>";	
			}
			else
			{
          		 echo "<li><a href='?page_no=$count'>$count</a></li>";
			}                  
        }
       
       	echo "<li><a>...</a></li>";
	   	echo "<li><a href='?page_no=$second_last'>$second_last</a></li>";
	  	echo "<li><a href='?page_no=$total_pages'>$total_pages</a></li>";      
    }
		
	else
	{
        echo "<li><a href='?page_no=1'>1</a></li>";
		echo "<li><a href='?page_no=2'>2</a></li>";
        echo "<li><a>...</a></li>";

        for ($count = $total_pages - 6; $count <= $total_pages; $count++)
        {
          	if($count == $page_no) 
          	{
		   		echo "<li class='active'><a>$count</a></li>";	
			}
			else
			{
           		echo "<li><a href='?page_no=$count'>$count</a></li>";
			}                   
        }
    }
}

?>
    
	<li <?php if($page_no >= $total_pages){ echo "class='disabled'"; } ?>>
	<a <?php if($page_no < $total_pages) { echo "href='?page_no=$next_page'"; } ?>>Next</a>
	</li>
    <?php if($page_no < $total_pages){
		echo "<li><a href='?page_no=$total_pages'>Last &rsaquo;&rsaquo;</a></li>";
		} ?>
</ul>
<br/><br/>
</div>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *