简体   繁体   English

CodeIgniter数据库搜索

[英]CodeIgniter Database Search

I have a search form which is used by staff to search for a user account. 我有一个搜索表单,工作人员可使用该表单搜索用户帐户。 I want the search form to take the input from the user and search the DB for a user that matches it. 我希望搜索表单接受用户的输入,并在数据库中搜索与其匹配的用户。 Here's my DB structure... 这是我的数据库结构...

Users Table -> (id, fname, lname, ip_address....etc.) 用户表->(id,fname,lname,ip_address等)

Here is what I'm using in my model to search for the user... 这是我在模型中用来搜索用户的内容...

$this->db->or_like('fname', $query);
$this->db->or_like('lname', $query);
$this->db->or_like('email', $query);

$this->db->or_where('ip_address', $query);

$query = $this->db->get('users');

This works great for most searches. 这对于大多数搜索而言效果很好。 For example.... 例如....

Search: Bill 搜索:Bill

returns all users with a first name or last name of bill..... 返回所有具有帐单名或姓氏的用户。

However, a search for a first and last name doesn't return a user even if there is a user that matches. 但是,即使有匹配的用户,搜索名字和姓氏也不会返回用户。 Example.... 例....

Search: Bill Gates 搜索:比尔·盖茨

That search doesn't return the user with the fname of Bill and the lname of Gates. 该搜索不会返回具有Bill的fname和Gates的lname的用户。 What can I do to make this work? 我该怎么做才能使这项工作?

You'll have to massage the search term into multiple individual terms. 您必须将搜索词分成多个单独的词。 CI will write the query as: CI将查询写为:

WHERE fname LIKE '%Bill Gates%' OR lname LIKE '%Bill Gates%' etc...

which will never match, since 'bill gates' never appears in a single field in the database. 由于“比尔·盖茨”永远不会出现在数据库的单个字段中,因此永远不会匹配。 But if you split it into multiple words, ( Bill and Gates ) and add each word to the or set individually, you will get matches. 但是,如果将其拆分为多个单词( BillGates )并将每个单词添加到or设置,则会得到匹配项。

The other option, if you're using MyISAM tables, is to use a fulltext index across the 3 fields, which would find 'bill gates', even if bill is one field and gates is in the other. 如果使用的是MyISAM表,则另一个选择是在3个字段之间使用全文索引,即使Bill是一个字段而Gates在另一个字段中,该索引也会找到“ bill gates”。

Apparently fulltext support for InnoDB is now actually finally in the development pipeline, but for now it's still myisam only in production code. 显然,现在对InnoDB的全文本支持实际上已经在开发管道中了,但是目前,它仍然仅在生产代码中是个谜。

Since you split the first and last name in the database and use the same variable for both or_like() calls, you can only search for one word. 由于您在数据库中拆分了名字和姓氏,并且为or_like()调用使用了相同的变量,因此只能搜索一个单词。

You have to split the string at " " (a space) and call or_like() for each part or use a form with 3 separate input fields (imho the better idea). 您必须在“”(空格)处分割字符串,并为每个部分调用or_like(),或使用具有3个单独输入字段的表单(恕我直言是更好的主意)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM