-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
88 lines (84 loc) · 3.12 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
//link : https://www.webslesson.info/2016/05/how-to-make-simple-pagination-using-php-mysql.html
$connect = mysqli_connect("localhost", "root", "", "demo_pagination");
$record_per_page = 5;
$page = '';
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $record_per_page;
$query = "SELECT * FROM tbl_student order by student_id DESC LIMIT $start_from, $record_per_page";
$result = mysqli_query($connect, $query);
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Pagination with Next Previous First Last page Link</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<style>
a {
padding:8px 16px;
border:1px solid #ccc;
color:#333;
font-weight:bold;
}
</style>
</head>
<body>
<br /><br />
<div class="container">
<h3 align="center">PHP Pagination with Next Previous First Last page Link</h3><br />
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th>ID</th>
<th>Student Name</th>
<th>Student Contact number</th>
</tr>
<?php
while ($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["student_id"]; ?></td>
<td><?php echo $row["student_name"]; ?></td>
<td><?php echo $row["student_phone"]; ?></td>
</tr>
<?php
}
?>
</table>
<div align="center">
<br />
<?php
$page_query = "SELECT * FROM tbl_student ORDER BY student_id DESC";
$page_result = mysqli_query($connect, $page_query);
$total_records = mysqli_num_rows($page_result);
$total_pages = ceil($total_records / $record_per_page);
$start_loop = $page;
$difference = $total_pages - $page;
if ($difference <= 5) {
$start_loop = $total_pages - 5;
}
$end_loop = $start_loop + 4;
if ($page > 1) {
echo "<a href='index.php?page=1'>First</a>";
echo "<a href='index.php?page=" . ($page - 1) . "'><<</a>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {
echo "<a href='index.php?page=" . $i . "'>" . $i . "</a>";
}
if ($page <= $end_loop) {
echo "<a href='index.php?page=" . ($page + 1) . "'>>></a>";
echo "<a href='index.php?page=" . $total_pages . "'>Last</a>";
}
?>
</div>
<br /><br />
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</body>
</html>