-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpagination
77 lines (61 loc) · 2.29 KB
/
pagination
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
<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$PHP_SELF = &$_SERVER['localhost/usefulbookdb/'];
// $PHP_SELF = &$_SERVER['PHP_SELF'];
$rec_limit = 10;
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($conn, 'test_db');
/* Get total number of records */
$sql = "SELECT count(emp_id) FROM employee ";
$retval = mysqli_query($conn, $sql );
if(! $retval ) {
die('Could not get data: ' . mysqli_error($conn));
}
//$row = $retval->fetch_assoc();
$row = mysqli_fetch_array($retval, MYSQLI_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) ) {
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}else {
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT emp_id, emp_name, emp_salary ".
"FROM employee ".
"LIMIT $offset, $rec_limit";
$retval = mysqli_query( $conn,$sql);
if(! $retval ) {
die('Could not get data: ' . mysqli_error($conn));
}
while( $row = $retval->fetch_assoc()) {
echo "EMP ID :{$row['emp_id']} <br> ".
"EMP NAME : {$row['emp_name']} <br> ".
" EMP SALARY : {$row['emp_salary']} <br> ".
"--------------------------------<br>";
}
if( $page > 0 ) {
$last = $page - 2;
echo "<a href = \"$PHP_SELF?page = $last\">Last 10 Records</a> |";
echo "<a href = \"$PHP_SELF?page = $page\">Next 10 Records</a>";
}else if( $page == 0 ) {
echo "<a href = \"$PHP_SELF?page = $page\">Next 10 Records</a>";
}else if( $left_rec < $rec_limit ) {
$last = $page - 2;
echo "<a href = \"$PHP_SELF?page = $last\">Last 10 Records</a>";
}
mysqli_close($conn);
?>
</body>
</html>