-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpagination_sample.php
More file actions
95 lines (89 loc) · 2.43 KB
/
Copy pathpagination_sample.php
File metadata and controls
95 lines (89 loc) · 2.43 KB
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
89
90
91
92
93
94
95
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$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>Webslesson Tutorial | 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" />
<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>
<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>
<td>Name</td>
<td>Phone</td>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<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='pagination.php?page=1'>First</a>";
echo "<a href='pagination.php?page=".($page - 1)."'><<</a>";
}
for($i=$start_loop; $i<=$end_loop; $i++)
{
echo "<a href='pagination.php?page=".$i."'>".$i."</a>";
}
if($page <= $end_loop)
{
echo "<a href='pagination.php?page=".($page + 1)."'>>></a>";
echo "<a href='pagination.php?page=".$total_pages."'>Last</a>";
}
?>
</div>
<br /><br />
</div>
</div>
</body>
</html>