-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.php
More file actions
63 lines (58 loc) · 2.54 KB
/
search.php
File metadata and controls
63 lines (58 loc) · 2.54 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
<?php
require __DIR__ . '/includes/bootstrap.php';
require __DIR__ . '/includes/header.php';
require_db();
$q = trim((string) ($_GET['q'] ?? ''));
$results = [];
$matches = 0;
if ($q !== '') {
$needle = '%' . $q . '%';
$stmt = $pdo->prepare('SELECT DISTINCT t.id, t.title, t.created_at, c.name AS category_name
FROM topics t
JOIN categories c ON c.id = t.category_id
LEFT JOIN posts p ON p.topic_id = t.id
WHERE t.deleted_at IS NULL AND (t.title LIKE ? OR p.content LIKE ?)
ORDER BY t.created_at DESC
LIMIT 50');
$stmt->execute([$needle, $needle]);
$results = $stmt->fetchAll();
$matches = count($results);
}
?>
<section class="bg-white p-4 rounded shadow-sm mb-4">
<div class="d-flex flex-column gap-2">
<h1 class="h4 mb-0">Recherche</h1>
<form method="get" action="search.php" class="d-flex flex-column flex-md-row gap-2">
<input class="form-control" type="search" name="q" placeholder="Rechercher un sujet, un message..." value="<?php echo e($q); ?>">
<button class="btn btn-primary" type="submit">Rechercher</button>
</form>
<?php if ($q !== ''): ?>
<div class="text-muted small"><?php echo e((string) $matches); ?> résultat<?php echo $matches > 1 ? 's' : ''; ?> pour “<?php echo e($q); ?>”.</div>
<?php endif; ?>
</div>
</section>
<div class="card shadow-sm">
<div class="card-header bg-white">
<strong>Résultats</strong>
</div>
<div class="list-group list-group-flush">
<?php if ($q === ''): ?>
<div class="list-group-item text-muted">Entrez une recherche pour afficher des résultats.</div>
<?php elseif (!$results): ?>
<div class="list-group-item text-muted">Aucun résultat.</div>
<?php else: ?>
<?php foreach ($results as $row): ?>
<a class="list-group-item list-group-item-action" href="topic.php?id=<?php echo e((string) $row['id']); ?>">
<div class="d-flex justify-content-between">
<div>
<h6 class="mb-1"><?php echo e($row['title']); ?></h6>
<small class="text-muted"><?php echo e($row['category_name']); ?></small>
</div>
<small class="text-muted"><?php echo e(format_date($row['created_at'])); ?></small>
</div>
</a>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<?php require __DIR__ . '/includes/footer.php'; ?>