Simple Pagination Using PHP MySQL

By | January 2, 2014

Webpage with more number of rowed data will make the page lengthier, pagination makes the long rows into small number of rows and remaining in next pages. Pagination in a webpage will make the webpage structure more crispy. Here is the tutorial about how to make simple pagination using php mysql

  

 

 

PHP Code

[code type=php]

<html>
<body>
<div id=”content”>
<?php
$query1=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“freeze_demo”,$query1);

$start=0;
$limit=10;

if(isset($_GET[‘id’]))
{
$id=$_GET[‘id’];
$start=($id-1)*$limit;
}

$query=mysql_query(“select * from pagination LIMIT $start, $limit”);
echo “<ul>”;
while($query2=mysql_fetch_array($query))
{
echo “<li>”.$query2[‘text1′].”</li>”;
}
echo “</ul>”;
$rows=mysql_num_rows(mysql_query(“select * from pagination”));
$total=ceil($rows/$limit);

if($id>1)
{
echo “<a href=’?id=”.($id-1).”‘ class=’button’>PREVIOUS</a>”;
}
if($id!=$total)
{
echo “<a href=’?id=”.($id+1).”‘ class=’button’>NEXT</a>”;
}

echo “<ul class=’page’>”;
for($i=1;$i<=$total;$i++)
{
if($i==$id) { echo “<li class=’current’>”.$i.”</li>”; }

else { echo “<li><a href=’?id=”.$i.”‘>”.$i.”</a></li>”; }
}
echo “</ul>”;
?>
</div>
</body>
</html>
[/code]

 

 

CSS Code

[code type=css]

#content
{
width: 900px;
margin: 0 auto;
font-family:Arial, Helvetica, sans-serif;
}
.page
{
float: right;
margin: 0;
padding: 0;
}
.page li
{
list-style: none;
display:inline-block;
}
.page li a, .current
{
display: block;
padding: 5px;
text-decoration: none;
color: #8A8A8A;
}
.current
{
font-weight:bold;
color: #000;
}
.button
{
padding: 5px 15px;
text-decoration: none;
background: #333;
color: #F3F3F3;
font-size: 13PX;
border-radius: 2PX;
margin: 0 4PX;
display: block;
float: left;
}

[/code]

 

10 thoughts on “Simple Pagination Using PHP MySQL

  1. Angel

    I have lo leave a comment becouse this really help me, the code is too simple and thats whats i like..
    Thanks a lots!
    I am going to click some ads to help u :p

    Reply
  2. Mohammad Zeus

    Hi Dear , I LOVE YOU , AND YOUR WEBSITE !!
    PLEASE KEEP ON WORK :-X

    Reply
  3. Rohir Naik

    Nice Article and i used it too.. But you made one mistake… you should correct it. 🙂
    You forgot to declare : $paging = 1;

    One can define it just above : if(isset($_GET[‘id’]))

    Eg:
    $paging = 1;
    if(isset($_GET[‘id’])){

    Reply

Leave a Reply to Rohir Naik Cancel reply

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