-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsearch.php
More file actions
executable file
·110 lines (100 loc) · 2.85 KB
/
search.php
File metadata and controls
executable file
·110 lines (100 loc) · 2.85 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
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
<?php
$connection_params = array(
"host" => "localhost",
"username" => "root",
"password" => "mike",
"database" => "search"
);
if (isset($_GET['search'])) {
//XXX: load data
$search = strip_tags($_GET['search']);
$search_term = trim($search);
$data = get_data();
$results = array_filter($data, function($val) use ($search_term) {
$exists = strripos($val['name'], $search_term);
if ($exists === FALSE) {
return false;
} else {
return true;
}
});
//XXX: Get a sorted array with the various ranks.
$sorted_results = get_sorted_results($results);
foreach ($sorted_results as $result){
$id = $result['id'];
$profile_data = array_filter($results, function($val) use ($id) {
return $val['id'] == $id;
});
//var_dump($profile_data);
// Display the data.
foreach( $profile_data as $profile){
//TODO: Load a template??
echo '<div id="search">';
echo '</br/>Name: <a href="profile.php?id=' . $profile['id'] . '">' . $profile['name'] . '</a>';
echo '</br/> Age:' . $profile['age'];
echo '</br/> Phone: ' . $profile['phone'];
echo '<div/>';
}
}
}
function get_sorted_results($profiles){
global $connection_params;
$username = $connection_params['username'];
$password = $connection_params['password'];
$database = $connection_params['database'];
$host = $connection_params['host'];
//TODO: Clean this db access...
$sql = "Select Keyword, Hits from KeyHits ";
$pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare($sql);
$stmt->execute();
$keywords = $stmt->fetchAll();
//xxx: Rank the chunkified keywords.
$new_profiles = array();
$sort_array = array();
foreach($profiles as $profile){
//1. Rank by both the names
$name_chunk = explode(' ', $profile['name']);
$rank = 0;
foreach($name_chunk as $name){
$hit = 0;
$result = array_filter($keywords, function($val) use ($name) {
if ($val['Keyword'] === $name) {
return true;
} else {
return false;
}
});
foreach($result as $val){
$hit = $val['Hits'];
}
// Increase the rank
$rank += $hit;
}
//TODO: 2. Rank by other profile details.
$sort_array[] = array('id' => $profile['id'], 'rank' => $rank);
}
// Sort the array by ranks descending
$ranks = array();
foreach($sort_array as &$value){
$ranks[] = &$value['rank'];
}
array_multisort($ranks, SORT_DESC, $sort_array);
return $sort_array;
}
function get_data() {
$string = file_get_contents("data/people.json");
$json_a = json_decode($string, true);
return $json_a['result'];
}
function array_results($val) {
$name = 'o';
$exists = strripos($val['name'], $name);
if ($exists === FALSE) {
return false;
} else {
return true;
}
}
?>