This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
search.php
67 lines (53 loc) · 1.54 KB
/
search.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
<?php
require_once 'header.php';
$page_title = '搜尋';
// die(var_dump($_SESSION));
$op = isset($_REQUEST['op']) ? filter_var($_REQUEST['op']) : '';
$keyword = isset($_REQUEST['keyword']) ? filter_var($_REQUEST['keyword']) : '';
/*************控制器**************/
switch ($op) {
case 'search':
search_article($keyword);
break;
default:
break;
}
require_once 'footer.php';
/*************函數區**************/
//讀出所有文章
function search_article($keyword)
{
global $db, $smarty;
if (empty($keyword)) {
header("location: index.php");
exit;
}
$keyword_arr = explode(' ', $keyword);
$key_arr = array();
foreach ($keyword_arr as $word) {
if (!empty($word)) {
$key_arr[] = "(`title` LIKE '%{$word}%' OR `content` LIKE '%{$word}%')";
}
}
$query = implode(' OR ', $key_arr);
$sql = "SELECT * FROM `article`
WHERE $query
ORDER BY `update_time` DESC";
include_once "PageBar.php";
$PageBar = getPageBar($db, $sql, 5, 10);
$bar = $PageBar['bar'];
$sql = $PageBar['sql'];
$total = $PageBar['total'];
$result = $db->query($sql) or die($db->error);
$all = array();
$i = 0;
while ($data = $result->fetch_assoc()) {
$all[$i] = $data;
$all[$i]['summary'] = mb_substr(strip_tags($data['content']), 0, 90);
$i++;
}
// die(var_export($all));
$smarty->assign('all', $all);
$smarty->assign('bar', $bar);
$smarty->assign('sql', $sql);
}