-
Notifications
You must be signed in to change notification settings - Fork 4
/
recherche.php
75 lines (58 loc) · 2.26 KB
/
recherche.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
<?php
//Initialisation de la variable contenant les résultats
$resultats = "";
$nbreParametres = 2; //Nombre de paramètres à renseigner
//Traitement de la requête
if(isset($_POST['query']) && !empty($_POST['query'])){
//Si l'utilisateur a entré quelque chose, on traite sa requête
//On rend clean la requête de l'utilisateur
$query = preg_replace("#[^a-z ?0-9]#i", "", $_POST['query']);
if($_POST['filtre'] == "Site entier"){
$nbreParametres = 4;
$sql = "(SELECT id, blog_title AS title FROM blog WHERE blog_title LIKE ? OR blog_content LIKE ?) UNION (SELECT id, page_title AS title FROM pages WHERE page_title LIKE ? OR page_content LIKE ?)";
} else if($_POST['filtre'] == "Blog") {
$sql = "SELECT id, blog_title AS title FROM blog WHERE blog_title LIKE ? OR blog_content LIKE ?";
} else if($_POST['filtre'] == "Pages") {
$sql = "SELECT id, page_title AS title FROM pages WHERE page_title LIKE ? OR page_content LIKE ?";
}
//Connexion à la base de données
include("includes/connect_db.php");
$req = $db->prepare($sql);
if($nbreParametres == 2){
$req->execute(array('%'.$query.'%', '%'.$query.'%'));
} else {
$req->execute(array('%'.$query.'%', '%'.$query.'%', '%'.$query.'%', '%'.$query.'%'));
}
$count = $req->rowCount();
if($count >= 1){
echo "$count résultat(s) trouvé(s) pour <strong>$query</strong><hr/>";
while($data = $req->fetch(PDO::FETCH_OBJ)){
echo '#'.$data->id.' - Titre: '.$data->title.'<br/>';
}
echo '<hr/>';
} else {
echo "0 résultat trouvé pour <strong>$query</strong><hr/>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Mini moteur de recherche</title>
<meta charset="UTF-8" />
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="query">Entrer votre recherche: </label>
<input type="search" name="query" maxlength="80" size="80" id="query" /><br/>
Rechercher au niveau de:
<select name="filtre">
<option value="Site entier">Site entier</option>
<option value="Blog">Blog</option>
<option value="Pages">Pages</option>
</select><br/>
<input type="submit" value="Rechercher">
</form>
<?php echo $resultats; ?>
</body>
</html>