forked from leoshtika/pagination
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-sqlite.php
More file actions
63 lines (52 loc) · 1.81 KB
/
Copy pathdemo-sqlite.php
File metadata and controls
63 lines (52 loc) · 1.81 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
<?php
require_once 'vendor/autoload.php';
use leoshtika\libs\Pagination;
use leoshtika\libs\Sqlite;
use leoshtika\libs\UserFaker;
$sqliteFile = 'demo.sqlite';
// Create a new sqlite db if not exists and load some dummy data.
// After the database is created, you don't need this line of code anymore
UserFaker::create($sqliteFile, 120);
$dbh = Sqlite::connect($sqliteFile);
// Get the total number of records
$totalRecords = $dbh->query('SELECT count(*) FROM user')->fetch(PDO::FETCH_COLUMN);
// Instantiate the Pagination
$pagination = new Pagination($_GET['page'], $totalRecords, 10);
// Get records using the pagination
$sth = $dbh->prepare('SELECT * FROM user LIMIT :offset, :records');
$sth->bindValue(':offset', $pagination->offset(), PDO::PARAM_INT);
$sth->bindValue(':records', $pagination->getRecordsPerPage(), PDO::PARAM_INT);
$sth->execute();
$users = $sth->fetchAll(PDO::FETCH_OBJ);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pagination</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>
<body class="container-fluid">
<h1>Pagination!</h1>
<table class="table table-bordered table-hover">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>Phone</th>
</tr>
<?php foreach ($users as $user) : ?>
<tr>
<td><?php echo $user->id; ?></td>
<td><?php echo $user->name; ?></td>
<td><?php echo $user->email; ?></td>
<td><?php echo $user->address; ?></td>
<td><?php echo $user->phone; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $pagination->nav(); ?>
</body>
</html>