How to create Pagination with PHP and MySql
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
}
$no_of_records_per_page = 10;
$offset = ($pageno-1) * $no_of_records_per_page;
$selectQuery = "SELECT id FROM tbl";
$resultselectCount = $db->query($selectQuery);
// count total rows
$total_rows = $resultselectCount->num_rows;
$query = "SELECT id FROM tbl LIMIT $offset, $no_of_records_per_page";
$selectdata = $db->query($query);
$total_pages = ceil($total_rows / $no_of_records_per_page);
if($pageno == '1'){
$count = 1;
}else{
$count = $pageno * 10 + 1 - 10;
}
while ($dataselectBanner = $selectdata->fetch_assoc()) {
// your stuff goes here...
}
?>
// Your Source for pagination
<nav>
<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="disabled"><a>Page <?php echo $pageno . " Of ". $total_rows/10;?></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>
</nav>
Comments
Post a Comment