-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautocomplete.php
105 lines (73 loc) · 2.64 KB
/
autocomplete.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
<?php
include_once('lib/php/fonction.php');
//Autocomplete
//on se connecte
$connection = connectBD();
if ($connection) {
try {
/* veillez bien à vous connecter à votre base de données */
$term = $_GET['term'];
$req = $connection->prepare('SELECT * FROM clients WHERE name LIKE :term || firstname LIKE :term ORDER BY id'); // j'effectue ma requête SQL grâce au mot-clé LIKE
$req->execute(array('term' => '%'.$term.'%'));
$array = array(); // on créé le tableau
while($data = $req->fetch()) // on effectue une boucle pour obtenir les données
{
$name = $data['name'];
$firstname = $data['firstname'];
$id = $data['id'];
//$data['id'] = $code;
$data["value"] = $name.' '.$firstname;
$data["label"] = $name.' '.$firstname.' '.$id;
//array_push($a_json, $a_json_row);
array_push($array, $data['label']); // et on ajoute celles-ci à notre tableau
}
$list = json_encode($array);
echo $list;
//json_encode($array); // il n'y a plus qu'à convertir en JSON
//echo $code;
}
//si erreur
catch(PDOException $e){
//on annule la transaction
$connection->rollback;
//on affiche les erreurs
$contenu = '<red> Erreur: Erreur lors de la requête, veuillez contacter votre administrateur!</red></br>';
$contenu .= '<red>Erreur: '.$e->getMessage().'</red></br>';
$contenu .= '<red>Erreur:'.$e->getCode().'</red></br>';
//on quitte, on arrête l'execution q'il y a du code après
exit();
}
}
else{
echo "imppossible de se connecter!";
}
/*$req = $connection->query('SELECT * FROM clients WHERE name LIKE "'.ucfirst($_REQUEST['term']).'%" ORDER BY id ASC LIMIT 0.10', $connection);
$data = array();
while ($row = mysql_fetch_assoc($req, MYSQL_ASSOC)) {
$data[] = array(
'label' => $row['name'],
'value' => $row['name']
);
# code...
}
$contenu = json_data($data);
flush();*/
/* retrieve the search term that autocomplete sends
$term = trim(strip_tags($_GET['term']));
$a_json = array();
$a_json_row = array();
if ($data = $connection->query("SELECT * FROM clients WHERE name LIKE '%$term%' OR firstname LIKE '%$term%' ORDER BY id")) {
while($row = mysqli_fetch_array($data)) {
$name = htmlentities(stripslashes($row['name ']));
$firstname = htmlentities(stripslashes($row['firstname']));
$id = htmlentities(stripslashes($row['id']));
$a_json_row["id"] = $code;
$a_json_row["value"] = $name.' '.$firstname;
$a_json_row["label"] = $name.' '.$firstname;
array_push($a_json, $a_json_row);
}
}
// jQuery wants JSON data
echo json_encode($a_json);
flush();*/
?>