search.php
1.64 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
<?php
/**
* Created by PhpStorm.
* User: aleksandarhristov
* Date: 14.09.15
* Time: 09:37
*/
//include the smarty class and the global functions
require "libs/Smarty.class.php";
require "global_functions.php";
//initialize smarty
$smarty = new Smarty;
//connect to the database
$db = new SQLite3('wiki.db');
if (isset($_GET['s']))
{
$intCtr=null;
$current_page=$db->querySingle("SELECT MIN (id) FROM pages");
if (!empty($_GET['s']))
{
$strSearch = " ".$_GET['s'];
$sql = "SELECT id, page_id, title, plain_text
FROM sections
WHERE upper(plain_text) LIKE upper('%$strSearch%')";
$result = $db->query($sql);
$i=0;
while ($row = $result->fetchArray())
{
$arrResult[$row[$i]] = array();
$arrResult[$row[$i]]['section_id'] = $row['id'];
$arrResult[$row[$i]]['page_id'] = $row['page_id'];
$arrResult[$row[$i]]['section_title'] = $row['title'];
$arrResult[$row[$i]]['section_text'] = "...".substr(stristr($row['plain_text'], $strSearch), 0, 300)."...";
$arrResult[$row[$i]]['section_text'] = preg_replace("/".preg_quote($strSearch)."\w*/i", "<mark><u><strong>$0</strong></u></mark>", $arrResult[$row[$i]]['section_text']);
}
if(empty($arrResult))
{
$error = "No result found!";
$arrResult = null;
}
else
{
$error = null;
}
$intCtr = count($arrResult);
}
//assign smarty variables and display the search.tpl
$smarty->assign("current_page", $current_page);
$smarty->assign("pages", get_pages());
$smarty->assign("pages_id", get_pages_id());
$smarty->assign("arrResult", $arrResult);
$smarty->assign("counter", $intCtr);
$smarty->assign("error", $error);
$smarty->display("tpl/search.tpl");
}