PHP/MySql“高级搜索"页面 [英] Php/ MySql 'Advanced Search' Page

查看:128
本文介绍了PHP/MySql“高级搜索"页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在网站上的高级搜索"页面上,您将在其中输入诸如我喜欢苹果"之类的关键字,并且它可以使用以下选项搜索数据库:

I'm working on an 'advanced search' page on a site where you would enter a keyword such as 'I like apples' and it can search the database using the following options:

查找:使用所有文字,使用 确切的词组,其中至少有一个 单词,没有单词

Find : With all the words, With the exact phrase , With at least one of the words, Without the words

我可以通过以下方式处理确切短语":

I can take care of the 'Exact phrase' by:

SELECT * FROM myTable WHERE field='$keyword';

至少一个单词"作者:

SELECT * FROM myTable WHERE field LIKE '%$keyword%';//Let me know if this is the wrong approach

但是我坚持使用的是至少有一个单词"和没有这些单词".

But its the 'With at least one of the words' and 'Without the words' that I'm stuck on.

关于如何实现这两个方面的任何建议?

Any suggestions on how to implement these two?

关于至少一个单词",使用explode()将关键字分解为单词并运行循环以添加

Regarding 'At least one word' it wouldn't be a good approach to use explode() to break the keywords into words, and run a loop to add

(field='$keywords') OR ($field='$keywords) (OR)....

因为查询中还存在其他AND/OR子句,但我不知道该子句的最大数目.

Because there are some other AND/OR clauses in the query also and I'm not aware of the maximum number of clauses there can be.

推荐答案

我建议使用布尔型全文本搜索功能,您应该能够获得所需的结果.

I would suggest the use of MySQL FullText Search using this with the Boolean Full-Text Searches functionality you should be able to get your desired result.

根据您所要求的条件提出的示例(它只是一个字段,他们可以选择4个选项中的任意一个(即1个单词,确切的单词,至少1个单词,不带术语).")

Requested example based on your requested conditions ("Its just one field and they can pick either of the 4 options (i.e 1 word, exact words, at least 1 word, without the term).")

我假设您是根据您的初始帖子使用php

I am assuming you are using php based on your initial post

<?php
$choice = $_POST['choice'];
$query = $_POST['query'];

if ($choice == "oneWord") {
    //Not 100% sure what you mean by one word but this is the simplest form
    //This assumes $query = a single word
    $result = mysql_query("SELECT * FROM table WHERE MATCH (field) AGAINST ('{$query}' IN BOOLEAN MODE)");
} elseif ($choice == "exactWords") {
    $result = mysql_query("SELECT * FROM table WHERE MATCH (field) AGAINST ('\"{$query}\"' IN BOOLEAN MODE)");
} elseif ($choice == "atLeastOneWord") {
    //The default with no operators if given multiple words will return rows that contains at least one of the words
    $result = mysql_query("SELECT * FROM table WHERE MATCH (field) AGAINST ('{$query}' IN BOOLEAN MODE)");
} elseif ($choice == "withoutTheTerm") {
    $result = mysql_query("SELECT * FROM table WHERE MATCH (field) AGAINST ('-{$query}' IN BOOLEAN MODE)");
}
?>

希望这有助于在布尔匹配中充分利用运算符,请参见布尔全文搜索

hope this helps for full use of the operators in boolean matches see Boolean Full-Text Searches

这篇关于PHP/MySql“高级搜索"页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆