-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsitesresult.php
174 lines (139 loc) · 6.08 KB
/
sitesresult.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
class SiteResultsProvider {
private $con;
/**public function filterSearchKeys($term){
$term = trim(preg_replace("/(\s+)+/", " ", $term));
$words = array();
// expand this list with your words.
$list = array("in","it","a","the","of","or","I","you","he","me","us","they","she","to","but","that","this","those","then");
$c = 0;
foreach(explode(" ", $term) as $key){
if (in_array($key, $list)){
continue;
}
$words[] = $key;
if ($c >= 15){
break;
}
$c++;
}
return $words;
}**/
public function __construct($con) {
$this->con = $con;
}
public function getNumResults($term) {
$query = $this->con->prepare("SELECT COUNT(*) as total
FROM sites WHERE title LIKE :term
OR url LIKE :term
OR keywords LIKE :term
OR description LIKE :term");
$searchTerm = "%". $term . "%";
$query->bindParam(":term", $searchTerm);
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
return $row["total"];
}
public function getResultsHtml($page, $pagesize, $term) {
$fromLimit = ($page - 1) * $pagesize;
$query = $this->con->prepare("SELECT *
FROM sites WHERE title LIKE :term
OR url LIKE :term
OR keywords LIKE :term
OR description LIKE :term
ORDER BY clicks DESC
LIMIT :fromLimit, :pagesize");
$searchTerm = "%". $term . "%";
$query->bindParam(":term", $searchTerm);
$query->bindParam(":fromLimit", $fromLimit, PDO::PARAM_INT);
$query->bindParam(":pagesize", $pagesize, PDO::PARAM_INT);
$query->execute();
//Weighing of the search results
/**$scoreFullTitle = 6;
$scoreTitleKeyword = 5;
$scoreFullDescription = 5;
$scoreDescriptionKeyword = 4;
$scoreKeywords = 3;
$scoreUrlKeyword = 1;
$queries = filterSearchKeys($term);
$escQuery = DB::escape($term);**/
$resultsHtml = "<div class='siteResult aqced'>";
while($row = $query->fetch(PDO::FETCH_ASSOC)){
/**$query = $this->con->prepare("SELECT
id,title,url,
description,keywords,
(
(-- Title score
".implode(" + ", $title)."
)+
(-- Description
".implode(" + ", $sumSQL)."
)+
(-- Keyword
".implode(" + ", $categorySQL)."
)+
(-- url
".implode(" + ", $urlSQL)."
)
) as relevance
FROM sites
WHERE title LIKE :term
OR url LIKE :term
OR keywords LIKE :term
OR description LIKE :term
ORDER BY clicks DESC
LIMIT :fromLimit, :pagesize");**/
$id = $row["id"];
$url = $row["url"];
$title = $row["title"];
$description = $row["description"];
$keywords = $row["keywords"];
//Code
/**if (count($queries) > 1){
$title = "if (title LIKE '%".$escQuery."%',{$scoreFullTitle},0)";
$description = "if (description LIKE '%".$escQuery."%',{$scoreFullDescription},0)";
}
foreach($queries as $key){
$title = "if (title LIKE '%".DB::escape($key)."%',{$scoreTitleKeyword},0)";
$description = "if (description LIKE '%".DB::escape($key)."%',{$scoreDescriptionKeyword},0)";
$url = "if (url LIKE '%".DB::escape($key)."%',{$scoreUrlKeyword},0)";
$keywords = "if ((SELECT count(keywords.id) FROM keywords JOIN keywords ON keywords.id = keywords.id WHERE keywords.id = id AND keywords.name = '".DB::escape($key)."') > 0,{$scoreKeywords},0)";
}
// Just incase it's empty, add 0
if (empty($title)){
$title = 0;
}
if (empty($description)){
$description = 0;
}
if (empty($url)){
$url = 0;
}
if (empty($keywords)){
$keywords = 0;
}**/
$title = $this->trimField($title, 50);
$description = $this->trimField($description, 198);
$url = $this->trimField($url, 58);
$keywords = $this->trimField($keywords, 25);
$resultsHtml .= "<div class='resultContainer'>
<h3 class='title'>
<a class='result' href='$url' data-linkId='$id'>
$title
</a>
</h3>
<span class='url'>$url</span>
<span class='description'>$description</span>
</div>";
//Code start Here
//Code End Here
}
$resultsHtml .= "</div>";
return $resultsHtml;
}
private function trimField($string, $characterLimit) {
$dots = strlen($string) > $characterLimit ? "..." : "";
return substr($string, 0, $characterLimit) . $dots;
}
}
?>