Skip to content

Instantly share code, notes, and snippets.

@AkaashSaini
Created November 19, 2022 04:19
Show Gist options
  • Save AkaashSaini/52a411ac798395c2e26397cbf2096d30 to your computer and use it in GitHub Desktop.
Save AkaashSaini/52a411ac798395c2e26397cbf2096d30 to your computer and use it in GitHub Desktop.
step 1 - create database (pagination)
step 2 - create table in (pagination) database called (record)
step 3 - create fields in (record) table (id,name)
step 4 - add 21-24 data rows in (record) table
step 5 - index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pagination Demo</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-3"></div>
<div class="col-6">
<?php
$connect = mysqli_connect('localhost', 'root', '', 'pagination');
if (!$connect) {
die("unable to connect to server");
}
//step - 4- get total number of record from table
$count = mysqli_num_rows(mysqli_query($connect, "select * from record"));
//step-1 how many record you want to show in one page
$per_page = 5;
//step-2 how many record do you have
$total_record = $count;
//step-3 how many pagination option show to user like 1,2,3,4
$pagination_options = ceil($total_record / $per_page); //ceil give us round figure (like 4.2 so it will give us 5 )
//step - 7- initialize new variable called ($start) default value 0.
$start = 0;
// step 10 make current page variable so u can style it default value is 1.
$current_page = 1;
// step 9 get the start value
if (isset($_GET['start'])) {
$start = $_GET['start'];
// step 12 if someone put zero or less then 0 on url.
if ($start <= 0) {
$start = 0;
$current_page = 1;
} else {
// step 9 part 2
$current_page = $start;
$start--;
$start *= 5;
}
}
//step-6- limit query to show data from 0 to 5 initially.
$query = "select * from record limit $start,$per_page";
$result = mysqli_query($connect, $query);
$n = mysqli_num_rows($result);
if ($n > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<ul class='list-group m-3'>
<li class='list-group-item'>$row[name]</li>
</ul>";
}
} else {
echo "no data available";
}
?>
<div class="pager">
<ul class="pagination justify-content-center">
<!-- step-5 show pagination option by for loop -->
<?php for ($i = 1; $i <= $pagination_options; $i++) { ?>
<!-- step 11 make current page active -->
<?php if ($current_page == $i) { ?>
<li class="page-item"><a class="page-link active" href="javascript:void(0)"><?php echo $i; ?></a></li>
<?php } else { ?>
<!-- step - 8 - change $start value on click -->
<li class="page-item"><a class="page-link" href="?start=<?php echo $i; ?>"><?php echo $i; ?></a></li>
<?php }
} ?>
</ul>
</div>
</div>
<div class="col-3"></div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment