-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
125 lines (90 loc) · 2.67 KB
/
index.php
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
123
124
125
<?php
require 'vendor/autoload.php';
require 'src/URLHelper.php';
use App\URLHelper;
define('PER_PAGE', 10);
$pdo = new PDO(
"sqlite:./sqliteData/chinook.db",
null,
null,
[
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]
);
$query = "SELECT * FROM customers ";
$querycount = "SELECT count(CustomerId) as count FROM customers ";
$params = [];
$page = (int)($_GET['p'] ?? 1);
$offset = ($page - 1) * PER_PAGE;
if (!empty($_GET['q'])) {
$query .= "WHERE Country like :Country";
$querycount .= "WHERE Country like :Country";
$params['Country'] = '%' . $_GET['q'] . '%';
}
$query .= " LIMIT " . PER_PAGE . " OFFSET " . $offset;
$statmentcount = $pdo->prepare($querycount);
$statmentcount->execute($params);
$statmentcount->execute();
$count = (int)$statmentcount->fetch()['count'];
$statment = $pdo->prepare($query);
$statment->execute($params);
$customers = $statment->fetchAll();
$nbpages = ceil($count / PER_PAGE);
//dd($nbpages);
$title = "Home";
require "elements/header.php";
?>
<div class="container">
<h2>Gest Book </h2>
<form action="">
<div class="form-group">
<input type="search" class="form-control" value="<?= htmlentities($_GET['q'] ?? '') ?>" name="q" placeholder="Recherche..." />
</div>
<div class="form-group">
<button class="btn btn-primary">
Recherche
</button>
</div>
</form>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">FirstName</th>
<th scope="col">LastName</th>
<th scope="col">Country</th>
</tr>
</thead>
<tbody>
<tr>
<?php foreach ($customers as $customer) : ?>
<th scope="row"><?= $customer['CustomerId'] ?></th>
<td><?= $customer['FirstName'] ?></td>
<td><?= $customer['LastName'] ?></td>
<td><?= $customer['Country'] ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php if ($nbpages > 1 && $page < $nbpages) : ?>
<a href=".?<?= URLHelper::withParam("p", ($page + 1)) ?>">Page suivante</a>
<?php endif ?>
<?php if ($nbpages > 1 && $page < $nbpages) : ?>
<?php for ($i = 1; $i <= $nbpages; $i++) : ?>
<?php if ($page === $i) : ?>
<?= $i ?>
<?php else : ?>
<a href=".?<?= URLHelper::withParam("p", ($i)) ?>"><?= $i ?></a>
<?php endif ?>
<?php endfor ?>
<?php endif ?>
<?php // if ($nbpages > 1 && $page > 1 ) :
?>
<?php if ($page > 1 && $page <= $nbpages) : ?>
<a href=".?<?= URLHelper::withParam("p", ($page - 1)) ?>">Page précedente</a>
<?php endif ?>
</div>
<?php
require "elements/footer.php";
?>