-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggest_data.php
56 lines (49 loc) · 1.2 KB
/
suggest_data.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
<?php
$people[] = "Steve";
$people[] = "John";
$people[] = "Kathy";
$people[] = "Evan";
$people[] = "Anthony";
$people[] = "Tom";
$people[] = "Rhonda";
$people[] = "Hal";
$people[] = "Ernie";
$people[] = "Johanna";
$people[] = "Farrah";
$people[] = "Linda";
$people[] = "Shawn";
$people[] = "Olivia";
$people[] = "Derek";
$people[] = "Amanda";
$people[] = "Rachel";
$people[] = "Katie";
$people[] = "Jillian";
$people[] = "Jose";
$people[] = "Malcom";
$people[] = "Greg";
$people[] = "Mary";
$people[] = "Brad";
$people[] = "Mike";
// Using '$_REQUEST' for handling both get and post requests
$query_string = $_REQUEST['q'];
$suggestion='';
if($query_string !== ''){
$q = strtolower($query_string);
$len = strlen($q);
foreach ($people as $person) {
# code...
// 'substr' is returning person array value from 0 to entered lenght of search query string
// 'stristr' checks if that matches the entered search query string
if(stristr($q , substr($person, 0,$len))){
if($suggestion===''){
// adding very first suggestion to the queue
$suggestion=$person;
}else{
// appending next suggestions
$suggestion.=', '.$person;
}
}
}
}
echo $suggestion ==='' ? 'No records found' : $suggestion;
?>