-
Notifications
You must be signed in to change notification settings - Fork 0
/
f12Class.php
executable file
·216 lines (188 loc) · 8.43 KB
/
f12Class.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
defined('ROOT') or define('ROOT', './');
require_once (ROOT . "configs/base.inc.php");
class f12Class extends BaseClass
{
//XX 用sphinx代替,该函数在s.php不再用之。
function select_contents_by_keyword($key) {
$this -> set_keywords($key);
$t = '';
$name = '';
if ($this -> lang == 'English') {
$t = 'All Records';
$name = 'Search - ';
} else {
$t = '所有记录';
$name = '搜索 - ';
}
$_SESSION[PACKAGE][SEARCH]['key'] = $key ? mysql_real_escape_string($key) : $t;
//计算对于此关键词,总共多少记录? $total=mysql_num_rows($res);
$total = $this -> get_contents_count($key);
$total_pages = ceil($total / ROWS_PER_PAGE);
$_SESSION[PACKAGE][SEARCH]['total'] = $total;
$_SESSION[PACKAGE][SEARCH]['total_pages'] = $total_pages;
//第一页:
$_SESSION[PACKAGE][SEARCH]['page'] = 1;
//当前从第几条记录开始显示?
$row_no = 0;
//生成新的查询语句�?
$lang_case = " and language = '" . $this -> lang . "' ";
/* select cid, title, date(created) as date, match(title, content) against('不理性行为' in boolean mode) as relevance
* from contents where match(title, content) against ('不理性行为' in boolean mode)
* and language = '中文' order by relevance desc limit 0,25
*
$sql = "select cid, title, date(created) as date,
MATCH(title, content) AGAINST('$key' in boolean mode) as relevancy
from contents
where MATCH(title, content) AGAINST('$key' in boolean mode) "
.$lang_case." order by relevancy desc";
*/
$sql = "select cid, title, date(created) as date from contents
where content like '%" . $key . "%' " . " or title like '%" . $key . "%' " . $lang_case . " order by cid desc";
$_SESSION[PACKAGE][SEARCH]['sql'] = $sql;
$sql .= " limit " . $row_no . "," . ROWS_PER_PAGE;
$ary = array();
$res = mysql_query($sql);
//mysql_num_rows($res)得到总行数,不需要两次查询: 去掉get_contents_count().
// echo $sql;
while ($row = mysql_fetch_assoc($res)) {
array_push($ary, $row);
}
mysql_free_result($res);
//返回生成的结果�?
return $ary;
}
// 还有用。
function set_keywords($key) {
//将关键词写入keywords表
if ($key != '') {
$user = isset($_SESSION[PACKAGE]['username']) ? $_SESSION[PACKAGE]['username'] : '';
$query = "INSERT INTO keywords (keyword,createdby,created) VALUES " . "('" . $key . "', '" . $user . "', now()) ON DUPLICATE KEY UPDATE total=total+1";
mysql_query($query);
/**
性能原因,tags表不用了。
$query = "insert into tags (name, createdby, created) values " . "('" . $key . "', '" . $user . "', now()) ON DUPLICATE KEY UPDATE total=total+1";
mysql_query($query);
*/
}
return true;
}
//XX 100%不能用,因为太慢了。
//select count(*) from contents where content like '%微笑局长%' or title like '%微笑局长%' and language='中文'
function get_contents_count($key) {
$sql = "select count(*) from contents
where content like '%" . $key . "%' " . " or title like '%" . $key . "%' and language='" . $this -> lang . "'";
$result = mysql_query($sql);
$num = mysql_fetch_row($result);
mysql_free_result($result);
return $num[0];
}
// ?page=2&q=&_=1352151347461
function draw()
{
$current_page = $_SESSION[PACKAGE][SEARCH]['page'] ? $_SESSION[PACKAGE][SEARCH]['page'] : 1;
$total_pages = $_SESSION[PACKAGE][SEARCH]['total_pages'] ? $_SESSION[PACKAGE][SEARCH]['total_pages'] : 1;
$links = array();
$queryURL = '';
if (count($_GET)) {
foreach ($_GET as $key => $value) {
if ( $key=='q' )
$queryURL .= '&' . $key . '=' . $value;
}
}
if (($total_pages) > 1) {
if ($current_page != 1) {
$links[] = '<a href="?page=1' . $queryURL . '">«« 首页 </a>';
$links[] = '<a href="?page=' . ($current_page - 1) . $queryURL . '">« 前页</a>';
}
for ($j = ($current_page - 4); $j < ($current_page + 4); $j++) {
if ($j < 1)
continue;
if ($j > $total_pages)
break;
if ($current_page == $j) {
$links[] = '<a href="javascript:;">' . $j . '</a>';
} else {
$links[] = '<a href="?page=' . $j . $queryURL . '">' . $j . '</a>';
}
}
if ($current_page < $total_pages) {
$links[] = '<a href="?page=' . ($current_page + 1) . $queryURL . '"> 下页 » </a>';
$links[] = '<a href="?page=' . ($total_pages) . $queryURL . '"> 末页 »» </a>';
}
return $links;
}
}
function draw_cate_item()
{
$current_page = isset($_SESSION[PACKAGE]['cate_item']['page']) ? $_SESSION[PACKAGE]['cate_item']['page'] : 1;
$total_pages = isset($_SESSION[PACKAGE]['cate_item']['total_pages']) ? $_SESSION[PACKAGE]['cate_item']['total_pages'] : 1;
$links = array();
if (($total_pages) > 1) {
if ($current_page != 1) {
$links[] = '<a href="?page=1">«« 首页 </a>';
$links[] = '<a href="?page=' . ($current_page - 1) . '">« 前页</a>';
}
for ($j = ($current_page - 4); $j < ($current_page + 4); $j++) {
if ($j < 1)
continue;
if ($j > $total_pages)
break;
if ($current_page == $j) {
$links[] = '<a href="javascript:;">' . $j . '</a>';
} else {
$links[] = '<a href="?page=' . $j . '">' . $j . '</a>';
}
}
if ($current_page < $total_pages) {
$links[] = '<a href="?page=' . ($current_page + 1) . '"> 下页 » </a>';
$links[] = '<a href="?page=' . ($total_pages) . '"> 末页 »» </a>';
}
return $links;
}
}
//还有用,网友在查。
function get_key_related($q) {
$sql = "select rid, rk, kurl from key_related where keyword like '%" . mysql_real_escape_string($q) . "%' order by rand() limit 0, " . TAB_LIST;
$res = $this -> mdb2 -> queryAll($sql, '', MDB2_FETCHMODE_ASSOC);
if (PEAR::isError($res)) {
die($res -> getMessage() . ' - line ' . __LINE__ . ': ' . $sql);
}
return $res;
}
// 输出内容.
function get_content_1($cid) {
$sql = "select * from contents where cid=" . $cid;
$res = mysql_query($sql);
$row = mysql_fetch_assoc($res);
mysql_free_result($res);
return $row;
}
function select_contents_by_page() {
//计算共有多少页?
$total_pages = isset($_SESSION[PACKAGE]['cate_item']['total_pages']) ? $_SESSION[PACKAGE]['cate_item']['total_pages'] : 1;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
if ($page > $total_pages)
$page = $total_pages;
if ($page < 1)
$page = 1;
$_SESSION[PACKAGE]['cate_item']['page'] = $page;
//当前从第几条记录开始显示?
$row_no = ((int)$page - 1) * ROWS_PER_PAGE;
//生成新的查询语句.
if (preg_match("/limit/i", $_SESSION[PACKAGE]['cate_item']['sql']))
$_SESSION[PACKAGE]['cate_item']['sql'] = preg_replace("/limit.*$/i", '', $_SESSION[PACKAGE]['cate_item']['sql']);
$sql = $_SESSION[PACKAGE]['cate_item']['sql'];
$sql .= " limit " . $row_no . "," . ROWS_PER_PAGE;
$_SESSION[PACKAGE]['cate_item']['sql'] = $sql;
$ary = array();
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) {
array_push($ary, $row);
}
mysql_free_result($res);
//返回生成的结果�?
return $ary;
}
}
?>