How to create Pagination with PHP ?



How to create Pagination with PHP ?

  • Paging means showing all your fetched results in multiple pages instead of display them all on one page. It makes that page so long and would take so much time to load.

How to create Pagination with PHP and MySql

  • MySQL’s LIMIT clause helps us to create pagination feature.
  • There are two arguments they are:
    • OFFSET
    • The number of records which will be returned from the database.

Following steps to create pagination in PHP

Get the current page number

  • This code will get the current page number with the help of $_GET Array. Note that if it is not present it will set the default page number to 1.
if (isset($_GET['pageno'])) 
{
    $pageno = $_GET['pageno'];
} 
else 
{
    $pageno = 1;
}

Formula for php pagination

  • The number of records to be displayed in a page by changing the value of $no_of_records_per_page variable.
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page; 

Constructing the SQL Query for pagination

$sql = "SELECT * FROM table LIMIT $offset, $no_of_records_per_page"; 

Pagination Buttons

  • These Buttons are served to users as Next Page & Previous Page, so that they can easily navigate through pages. Using bootstrap’s pagination button.
<ul class="pagination">
    <li><a href="?pageno=1">First</a></li>
    <li class="<?php if($pageno <= 1){ echo 'disabled'; } ?>">
        <a href="<?php if($pageno <= 1){ echo '#'; } else { echo "?pageno=".($pageno - 1); } ?>">Prev</a>
    </li>
    <li class="<?php if($pageno >= $total_pages){ echo 'disabled'; } ?>">
        <a href="<?php if($pageno >= $total_pages){ echo '#'; } else { echo "?pageno=".($pageno + 1); } ?>">Next</a>
    </li>
    <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
</ul>

Sample Code

<link href="bootstrap3.css" rel="stylesheet">  
<?php  

$dbhost = 'localhost';  
$dbuser = 'root';  
$dbpass = ";  
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');  
$dbname = 'test';  
$connection = mysql_select_db($dbname);  
  
$limit = 2;  
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };  
$start_from = ($page-1) * $limit;  
  
$sql = "SELECT * FROM posts ORDER BY title ASC LIMIT $start_from, $limit";  
$rs_result = mysql_query ($sql);  
?>  
<table class="table table-bordered table-striped">  
<thead>  
<tr>  
<th>title</th>  
<th>body</th>  
</tr>  
<thead>  
<tbody>  
<?php  
while ($row = mysql_fetch_assoc($rs_result)) {  
?>  
            <tr>  
            <td><? echo $row["title"]; ?></td>  
            <td><? echo $row["body"]; ?></td>  
            </tr>  
<?php  
};  
?>  
</tbody>  
</table>  
<?php  
$sql = "SELECT COUNT(id) FROM posts";  
$rs_result = mysql_query($sql);  
$row = mysql_fetch_row($rs_result);  
$total_records = $row[0];  
$total_pages = ceil($total_records / $limit);  
$pagLink = "<div class='pagination'>";  
for ($i=1; $i<=$total_pages; $i++) {  
             $pagLink .= "<a href='index.php?page=".$i."'>".$i."</a>";  
};  
echo $pagLink . "</div>";  
?>


Related Searches to How to create Pagination with PHP ?