-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path03-pagination.php
More file actions
123 lines (101 loc) · 3.49 KB
/
03-pagination.php
File metadata and controls
123 lines (101 loc) · 3.49 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* Example: Pagination
*
* Demonstrates LIMIT and OFFSET for paginated results
*/
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../helpers.php';
use tommyknocker\pdodb\helpers\Db;
$db = createExampleDb();
$driver = getCurrentDriver($db);
echo "=== Pagination Example (on $driver) ===\n\n";
// Setup with 50 users
$schema = $db->schema();
recreateTable($db, 'users', [
'id' => $schema->primaryKey(),
'name' => $schema->text(),
'email' => $schema->text(),
'created_at' => $schema->datetime()->defaultExpression('CURRENT_TIMESTAMP'),
]);
echo "1. Inserting 50 users...\n";
$users = [];
for ($i = 1; $i <= 50; $i++) {
$users[] = [
'name' => "User $i",
'email' => "user$i@example.com"
];
}
$db->find()->table('users')->insertMulti($users);
echo "✓ Inserted 50 users\n\n";
// Example 2: Simple pagination
echo "2. Paginating results (10 per page)...\n";
$perPage = 10;
$page = 1;
for ($page = 1; $page <= 3; $page++) {
$offset = ($page - 1) * $perPage;
$results = $db->find()
->from('users')
->select(['id', 'name', 'email'])
->orderBy('id')
->limit($perPage)
->offset($offset)
->get();
echo " Page $page:\n";
foreach ($results as $user) {
echo " • #{$user['id']}: {$user['name']}\n";
}
echo "\n";
}
// Example 3: Calculate total pages
echo "3. Pagination metadata...\n";
$totalUsers = $db->find()->from('users')->select([Db::count()])->getValue();
$totalPages = ceil($totalUsers / $perPage);
echo " Total users: $totalUsers\n";
echo " Per page: $perPage\n";
echo " Total pages: $totalPages\n\n";
// Example 4: Pagination function
function getPaginatedResults($db, $page, $perPage = 10) {
$offset = ($page - 1) * $perPage;
$results = $db->find()
->from('users')
->select(['id', 'name', 'email'])
->orderBy('id')
->limit($perPage)
->offset($offset)
->get();
$total = $db->find()->from('users')->select([Db::count()])->getValue();
return [
'data' => $results,
'pagination' => [
'current_page' => $page,
'per_page' => $perPage,
'total' => $total,
'total_pages' => ceil($total / $perPage),
'has_next' => $page < ceil($total / $perPage),
'has_prev' => $page > 1
]
];
}
echo "4. Using pagination function...\n";
$result = getPaginatedResults($db, 3, 10);
echo " Page {$result['pagination']['current_page']} of {$result['pagination']['total_pages']}\n";
echo " Showing: " . count($result['data']) . " users\n";
echo " Has next: " . ($result['pagination']['has_next'] ? 'Yes' : 'No') . "\n";
echo " Has previous: " . ($result['pagination']['has_prev'] ? 'Yes' : 'No') . "\n";
echo " Users on this page: " . implode(', ', array_column($result['data'], 'name')) . "\n\n";
// Example 5: Last page
echo "5. Jumping to last page...\n";
$lastPage = $totalPages;
$result = getPaginatedResults($db, $lastPage, 10);
echo " Last page ({$result['pagination']['current_page']}):\n";
foreach ($result['data'] as $user) {
echo " • {$user['name']}\n";
}
echo " Total on last page: " . count($result['data']) . "\n";
echo "\nPagination example completed!\n";
echo "\nKey Takeaways:\n";
echo " • Always use LIMIT and OFFSET together\n";
echo " • Calculate total pages: ceil(total / perPage)\n";
echo " • Offset = (page - 1) * perPage\n";
echo " • Include ORDER BY for consistent results\n";