Easy simple pagination using php and mysql

Easy simple pagination using php and mysql

Pagination is simply a process of dividing your output contents in small modules and displaying them on different pages.
For example,you can see Google search results where 10 to 15 search results are shown per page.
Pagination make output more user friendly.

Database Connectivity CODE

<?php
ob_start();
session_start();
$con=mysqli_connect("localhost","root","") or die(mysqli_error());
mysqli_select_db($con,"practice") or die(mysqli_error($con));
?>

CSS CODE

<style>
@import url(http://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100);

body {
 background-color: #3e94ec;
 font-family: "Roboto", helvetica, arial, sans-serif;
 font-size: 16px;
 font-weight: 400;
 text-rendering: optimizeLegibility;
}

div.table-title {
 display: block;
 margin: auto;
 max-width: 600px;
 padding:5px;
 width: 100%;
}

.table-title h3 {
 color: #fafafa;
 font-size: 30px;
 font-weight: 400;
 font-style:normal;
 font-family: "Roboto", helvetica, arial, sans-serif;
 text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
 text-transform:uppercase;
}


/*** Table Styles **/

.table-fill {
 background: white;
 border-radius:3px;
 border-collapse: collapse;
 height: 320px;
 margin: auto;
 max-width: 600px;
 padding:5px;
 width: 100%;
 box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
 animation: float 5s infinite;
}
 
th {
 color:#D5DDE5;;
 background:#1b1e24;
 border-bottom:4px solid #9ea7af;
 border-right: 1px solid #343a45;
 font-size:23px;
 font-weight: 100;
 padding:24px;
 text-align:left;
 text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
 vertical-align:middle;
}

th:first-child {
 border-top-left-radius:3px;
}
 
th:last-child {
 border-top-right-radius:3px;
 border-right:none;
}
 
tr {
 border-top: 1px solid #C1C3D1;
 border-bottom-: 1px solid #C1C3D1;
 color:#666B85;
 font-size:16px;
 font-weight:normal;
 text-shadow: 0 1px 1px rgba(256, 256, 256, 0.1);
}
 

 
tr:first-child {
 border-top:none;
}

tr:last-child {
 border-bottom:none;
}
 
tr:nth-child(odd) td {
 background:#EBEBEB;
}
 


tr:last-child td:first-child {
 border-bottom-left-radius:3px;
}
 
tr:last-child td:last-child {
 border-bottom-right-radius:3px;
}
 
td {
 background:#FFFFFF;
 padding:20px;
 text-align:left;
 vertical-align:middle;
 font-weight:300;
 font-size:18px;
 text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
 border-right: 1px solid #C1C3D1;
}

td:last-child {
 border-right: 0px;
}

th.text-left {
 text-align: left;
}

th.text-center {
 text-align: center;
}

th.text-right {
 text-align: right;
}

td.text-left {
 text-align: left;
}

td.text-center {
 text-align: center;
}

td.text-right {
 text-align: right;
}
td a{font-size:16px; color:#FFF; margin-left:5px; padding:5px 10px; text-decoration:none; background:#333333;}
</style>

PHP Pagination CODE

<?php
    $start=0;
    $limit=3;
    $id=0;
    if(isset($_GET['id']))
    {
        $id=$_GET['id'];
        $start=($id-1)*$limit;
    }
     $sql=mysqli_query($con, "select * fro m student ORDER BY `id` DESC LIMIT $start, $limit") or die(mysqli_error($con));

?>
<table align='center' class='table-fill'>
    <tr>
        <th>Name</th>
        <th>Email Id</th>
        <th>Phone Number</th>
    </tr>
    <?php
            
        while($row=mysqli_fetch_assoc($sql))
        {
        
    ?>
    <tr>
        <td><?php echo $row['name'];?></td>
        <td><?php echo $row['email_id']?></td>
        <td><?php echo $row['phone_no']?></td>
    </tr>
    <?php
    
        }
    ?>
            
    <tr>
        <td colspan='3'>
        
    <?php
        
    $rows=mysqli_num_rows(mysqli_query($con, "select * fro m student"));
    $total=ceil($rows/$limit);
    if(isset($id))
    {
        if($id>1)
        {
            echo "<a href='?id=".($id-1)."' class='paging'>PREVIOUS</a>";
        }
    }

    for($i=1;$i<=$total;$i++)
    {
        echo "<a class='paging' href='?id=".$i."'>".$i."</a>"; 
    }
    
    if($id!=$total)
    {
        echo "<a class='paging' href='?id=".($id+1)."' class=paging''>NEXT</a>";
    }
    
    ?>
    </td>
    </tr>
</table> 

Click on Image and download Source Code of This Tutorial

Download source code of Simple jQuery Popup Window registration form On Page load

I hope you like this Article. Share with your friends.

About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.