How to Simple and Best Pagination using PHP and MySQL
Pagination is useful to show large data records into chunks of data. Its very easy to apply pagination. Its very basic tutorial to teach you how to apply pagination. So you have to give start point and a limit. “Select * from table Limit {starting Point}, Limit”. You may also like How to Ajax Pagination in PHP, How to Ajax Pagination in Codeigniter and How to Ajax Pagination in Laravel 5
PHP Code
<?php
define('DB_SERVER', "localhost");
define('DB_USER', "root");
define('DB_PASS', "");
define('DB_DATABASE', "whatsappdwld_db");
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$page_result = 10;
if ($_GET['pageno']) {
$page_value = $_GET['pageno'];
if ($page_value > 1) {
$offset = ($page_value - 1) * $page_result;
}
}
else
{
$offset = 0;
}
$select_results = " SELECT * FROM student_info LIMIT $offset, $page_result ";
$result = mysqli_query($con, $select_results);
while ($row = mysqli_fetch_assoc($result)) {
echo $row[' student_name '];
echo $row[' student_rollno '];
echo $row[' student_course '];
}
$select_results_count = " SELECT * FROM student_info";
$result_count = mysqli_query($con, $select_results_count);
$pagecount = mysqli_num_rows($result_count);
$num = $pagecount / $page_result;
if ($_GET['pageno'] > 1) {
echo "<a href = 'samepage.php?pageno = " . ($_GET['pageno'] - 1) . " '> Prev </a>";
}
for ($i = 1; $i <= $num; $i++) {
echo "<a href='samepage.php?pageno=".$i."'>".$i."</a> ";
}
if($num != 1)
{
echo "<a href = 'samepage.php?pageno = ".($_GET['pageno'] + 1)." ' > Next </a>";
}
?>
Leave a Reply