开发者

searching mysql using php, how to produce (and, or, not) functionality in users' search string?

I am making a search function on a website to let users search values held in a mysql database.

As an example:

There are two products in this table.

One has a description value of "coke can 330ml" and the other "sprite can 330ml".

Now when searching for these items, say a user enters can as their string. Both items are displayed because they both contain can.

If the user enters coke, the coke can will be returned.

Now imagine that you had every can of soda ever made inside the database, each as a product.

Search functionality like this, would be a bit troublesome. say a user wanted only two products, these being the coke and sprite cans.

They would need to search something like "coke OR sprite AND can AND 330ml"

This should only retur开发者_JS百科n the two products used in my example.

Not the problem that i face is this. My search feature runs like my original example. I am trying to include some kind of and, or, not functionality, and have not quite decided how this should all work out.

Currently, a seach of "coke can" will return only the results containing coke and can. But if I searched "coke sprite can" No results will be returned as there are no results that contain all 3 of the words used in the string.

Here is the query:

$dataQuery = "
         SELECT *
         FROM `products`
         WHERE upper(`desc`)
         LIKE '%"
             . implode("%'
         AND upper(`desc`)
         LIKE '%",
             $keywords_array) 
             . "%'
         ORDER BY `desc`";

$keywords_array is an array built up from the user entered search string, after validation, the code for this is as follows:

$find = strtoupper($find); 
$find = strip_tags($find); 
$find = trim ($find);
$_SESSION['find'] = $find;
$keywords_array = explode(' ', $find);

$find is the string set by the post value of the search string entered by the user.

Does anyone have any ideas on a good way to accomplish what I have explained?

If you need any additional code, or a better explanation, please just ask!

Thanks!!


You are looking for mysql fulltext search with the limitation that it has a default minimum word length of 4 characters, so you can't search for "can" if you can't edit the server configuration.

Also, it has a "stop word" policy that doesn't deliver results on words that appear in more than 50% of the overall data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜