如果有人审查我的代码,我需要知道更好的解决方案或任何错误。
好的,我制作了一个PHP文件,通过参数获得搜索结果,并通过标题、内容和标记进行搜索
PHP\'s head look like:
include(\'../../../../wp-config.php\');
header(\'Content-type:text/html;charset=utf-8\');
$keyword = $_GET[\'k\'];
之后我将通过jQuery带来搜索结果。
显示数据库结果的主要搜索功能:
function search($keyword,$mark){
while($data=mysql_fetch_array($keyword)){
if($data[\'post_status\']==\'publish\'){
echo \'<li>#\'.$data[\'ID\'].\' \'.htmlentities($data[\'post_title\']).\' (\'.$mark.\')</li>\';
}
}
}
Search by title function : 完整关键字(例如:“jack daniels”)
$sql_full_keyword = "SELECT * from wp_posts WHERE post_title LIKE \'$keyword%\'";
$search_full_keyword = mysql_query($sql_full_keyword) or die(mysql_error());
search($search_full_keyword,"Full keyword(s) = \'$keyword\'");
因此,我将关键字拆分为以下步骤:
$keywords_split = explode(" ",$keyword);
Search by title function : 多关键字(例如:jack+daniels)
for($i=0;$i<count($keywords_split);$i++){
$sql_split_keyword = "SELECT * from wp_posts WHERE post_title LIKE \'$keywords_split[$i]%\'";
$search_split_keyword = mysql_query($sql_split_keyword) or die(mysql_error());
search($search_split_keyword,"Split keyword = \'$keywords_split[$i]\'");
}
Search by content function : (同上,但按内容搜索)
for($i=0;$i<count($keywords_split);$i++){
$sql_split_keyword = "SELECT * from wp_posts WHERE post_content LIKE \'$keywords_split[$i]%\'";
$search_split_keyword = mysql_query($sql_split_keyword) or die(mysql_error());
search($search_split_keyword,"Split keyword = \'$keywords_split[$i]\' (Content)");
}
然后关闭mysql
mysql_close();
Search by tag function : (使用普通wordpress查询)
$tags = str_replace(" ","-",$keyword).",".str_replace(" ",",",$keyword);
query_posts(array(\'post_type\'=>array(\'one\',\'two\',\'three\') ,\'tag\' => $tags,\'posts_per_page\'=>-1));
while (have_posts()):the_post();
echo \'<li>#\'.get_the_ID().\' \'.get_the_title().\'</li>\';
endwhile;
现在一切正常,我需要有人检查我的代码,告诉我更好的事情或tong。。。
此处为完整代码:http://snipt.org/zpz0
谢谢