001.
002.
003.
require
'Rundiz/Pagination/Pagination.php'
;
004.
005.
006.
mysqli_report(MYSQLI_REPORT_STRICT);
007.
try {
008.
$mysql
=
new
\mysqli(
$db
[
'host'
],
$db
[
'username'
],
$db
[
'password'
],
$db
[
'dbname'
],
$db
[
'port'
]);
009.
} catch (\Exception
$e
) {
010.
echo
'MySQL connect error ('
.
$e
->getCode().
') '
.
$e
->getMessage().
'<br>'
.
"\n"
;
011.
echo
'Please make sure that you had already configure database settings in this file. '
.
__FILE__
.
"\n"
;
012.
exit
;
013.
}
014.
$mysql
->set_charset(
'utf8'
);
015.
016.
017.
018.
$sql
=
'SELECT * FROM `'
.
$db
[
'tablename'
].
'`'
;
019.
$result
=
$mysql
->query(
$sql
);
020.
if
(
$result
=== false) {
021.
printf(
"Error: %s\n"
,
$mysql
->error);
022.
exit
;
023.
}
024.
025.
$total_records
=
$result
->num_rows;
026.
$start
= (isset(
$_GET
[
'start'
]) ?
intval
(
$_GET
[
'start'
]) : 0);
027.
if
(
$start
< 0) {
028.
$start
= 0;
029.
}
030.
$limit
= 10;
031.
$offset
=
$start
;
032.
033.
034.
$result
->free();
035.
$sql
.=
' LIMIT '
.
$limit
.
' OFFSET '
.
$offset
;
036.
$result
=
$mysql
->query(
$sql
);
037.
if
(
$result
=== false) {
038.
printf(
"Error: %s\n"
,
$mysql
->error);
039.
exit
;
040.
}
041.
unset(
$sql
);
042.
043.
044.
$mysql
->close();
045.
unset(
$mysql
);
046.
047.
048.
049.
$base_url
= (isset(
$_SERVER
[
'HTTPS'
]) &&
$_SERVER
[
'HTTPS'
] ?
'https://'
:
'http://'
) .
$_SERVER
[
'HTTP_HOST'
] .
$_SERVER
[
'PHP_SELF'
] .
'?start=%PAGENUMBER%'
;
050.
051.
052.
$query_string_array
=
array
();
053.
if
(isset(
$_GET
) &&
is_array
(
$_GET
)) {
054.
foreach
(
$_GET
as
$key
=>
$value
) {
055.
if
(
$key
!=
'start'
) {
056.
$query_string_array
[
$key
] =
$value
;
057.
}
058.
}
059.
unset(
$key
,
$value
);
060.
}
061.
if
(!
empty
(
$query_string_array
)) {
062.
$base_url
.=
'&'
. http_build_query(
$query_string_array
, null,
'&'
);
063.
}
064.
unset(
$query_string_array
);
065.
066.
067.
$Pagination
=
new
\Rundiz\Pagination\Pagination();
068.
069.
$Pagination
->base_url =
$base_url
;
070.
$Pagination
->total_records =
$total_records
;
071.
$Pagination
->page_number_value =
$start
;
072.
?>
073.
<!-- สมมุติว่าเรามีหน้า html ทั้งหน้าแล้ว อันนี้เป็นแค่ส่วนแสดงผลตรงตาราง ผมจึงขอแสดงตัวอย่างแค่นี้นะครับ -->
074.
<table
class
=
"table"
>
075.
<thead>
076.
<tr>
077.
<th style=
"width: 10%;"
>ID</th>
078.
<th>Name</th>
079.
<th>Gender</th>
080.
<th>Email</th>
081.
<th>IP Address</th>
082.
</tr>
083.
</thead>
084.
<tfoot>
085.
<tr>
086.
<th>ID</th>
087.
<th>Name</th>
088.
<th>Gender</th>
089.
<th>Email</th>
090.
<th>IP Address</th>
091.
</tr>
092.
</tfoot>
093.
<tbody>
094.
<?php
095.
while
(
$item
=
$result
->fetch_assoc()) {
096.
?>
097.
<tr>
098.
<td><?php
echo
$item
[
'id'
]; ?></td>
099.
<td><?php
echo
$item
[
'first_name'
].
' '
.
$item
[
'last_name'
]; ?></td>
100.
<td><?php
echo
$item
[
'gender'
]; ?></td>
101.
<td><?php
echo
$item
[
'email'
]; ?></td>
102.
<td><?php
echo
$item
[
'ip_address'
]; ?></td>
103.
</tr>
104.
<?php
105.
}
106.
$result
->free();
107.
unset(
$item
,
$result
);
108.
?>
109.
</tbody>
110.
</table>
111.
<?php
112.
113.
echo
$Pagination
->createLinks();
114.
?>