How to create simple pagination using CSS and PHP

02nd, January 2018 By

We need pagination lot of times when we are doing code in php.Here i have tried to do the pagination as much simple so its easy to understand and implement.

Simple Pagination

The CSS


.user
{
padding:10px;
border-bottom:dashed 1px #5d478b;
width:450px;
margin-bottom:15px;
}

#pagination
{
clear:both;
list-style-type:none;
overflow:hidden;
margin:0;
padding:0;
}

#pagination li
{
float:left;
}

#pagination li a
{
font-weight:bold;
margin:0 4px;
padding:5px 10px;
border:1px solid #bbb;
display:inline-block;
border-radius:5px;
}

#pagination li a:hover
{
text-decoration:none;
color:#ff0080;
}

The PHP

You can create a file like pagination.php and copy paste given code in the your file or already created file.Change the name of the table and variable as your requirement.

<?php
include ("database.php");
$result=mysql_query("select count(*) as total from user");
$row=mysql_fetch_array($result);
$totalRows=$row['total'];
$rowsPerPage=10;
//rows per page,declare it as per your requirement
$pageNumber=1;
//page number,setting default as 1
if(isset($_GET['pg']))
{
$pageNumber=$_GET['pg'];
}
$totalpages=($totalRows/$rowsPerPage);
//The total pages for pagination
if($totalRows%$rowsPerPage>0)
{
$totalpages++;
}
$from=(($pageNumber-1)*$rowsPerPage)+1;
$to=($pageNumber)*($rowsPerPage);
$result=mysql_query("select * from user ORDER BY ID DESC Limit $from, $rowsPerPage");
while($row=mysql_fetch_row($result))
{
echo "<div class='user'>$row[1]</div>";
}
//Your Pagination at bottom
echo "<ul id='pagination'>";
for($i=1;$i<=$totalpages;$i++)
{
echo "<li><a href='pagination.php?pg=$i'>$i</a></li>";
}
echo "</ul>";
?></div>

Leave a Reply

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