如何在 mysql codeigniter php 中使用 select 计算匹配的关键字?
how to count the matched keywords using select in mysql codeigniter php?
如何在mysql codeigniter php中使用select统计匹配关键字?这是我的 table
例如。假设搜索关键词是,test
messages table
id | title | msg |date
---+-----------+-------------------+-------------+--
1 | test1 | yes | 2016-06-01 // 2 match keywords
2 | yes1 | no | 2016-06-02 // 1 match keywords
3 | test2 | no | 2016-06-03 // 1 match keywords
4 | yes2 | yes yes yes | 2016-06-04 // 4 match keywords
5 | yesyes3 | yes yes yes yes | 2016-06-05 // 6 match keywords
现在需要用 count_match 列显示它
id | title | msg |date |count_match
---+-----------+-------------------+------------+-------------------
1 | test1 | yes | 2016-06-01 | 2
2 | yes1 | no | 2016-06-02 | 1
3 | test2 | no | 2016-06-03 | 1
4 | yes2 | yes yes yes | 2016-06-04 | 4
5 | yesyes3 | yes yes yes yes | 2016-06-05 | 6
对于数组,它应该显示如下
array (
[0] => array (
[id] => 5
[title] => yesyes3
[msg] => yes yes yes yes
[date] => 2016-06-05
[match] => 6
)
[1] => array (
[id] => 4
[title] => yes2
[msg] => yes yes yes
[date] => 2016-06-04
[match] => 4
)
[2] => array (
[id] => 1
[title] => test1
[msg] => yes
[date] => 2016-06-01
[match] => 2
)
[3] => array (
[id] => 3
[title] => test2
[msg] => no
[date] => 2016-06-03
[match] => 1
)
[4] => array (
[id] => 2
[title] => yes1
[msg] => no
[date] => 2016-06-02
[match] => 1
)
)
目前我为此获取的代码是这样的
$match = array('test','yes');
$orderbyString1 = "";
$orderbyString = "";
$likestr = "";
foreach($match AS $value)
{
$orderbyString .= "IF(m.title LIKE '%".$value."%' OR m.msg LIKE '%".$value."%',1,0)+";
$likestr .= "m.title LIKE '%".$value."%' OR m.msg LIKE '%".$value."%' OR ";
}
$orderbyString = substr($orderbyString, 0, -1);
$likestr = substr($likestr, 0, -4);
$this->db->select('m.*, ('.$orderbyString.') as count_match');
$this->db->from('messages m');
$this->db->where($likestr." ORDER BY ".$orderbyString." DESC");
$query = $this->db->get();
$results = $query->result_array();
print_r($results);exit;
有什么方法可以让 select 使用 php codeigniter 代码显示正确的 count_match 吗?因为目前它在 count_match.
下显示 1 和 2
谢谢!
在 php 方面,有很多选项可以计算数组中的关键字。如果您需要其他功能,例如无大小写匹配或 word boundaries how about using regex.
一个想法preg_match_all
Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.
$pattern = '~(?:yes|test)~i';
foreach($arr AS $k => $v)
$arr[$k]['match'] = preg_match_all($pattern, $v['title']." ".$v['msg']);
模式只是一个 alternation of the two keywords using a non-capturing group. After the closing pattern delimiter ~
used the i
flag for caseless matching. Regex101 是测试模式的好地方。
如果输入是通用的,请使用 preg_quote
从其特殊的正则表达式含义中转义某些字符。
如何在mysql codeigniter php中使用select统计匹配关键字?这是我的 table
例如。假设搜索关键词是,test
messages table
id | title | msg |date
---+-----------+-------------------+-------------+--
1 | test1 | yes | 2016-06-01 // 2 match keywords
2 | yes1 | no | 2016-06-02 // 1 match keywords
3 | test2 | no | 2016-06-03 // 1 match keywords
4 | yes2 | yes yes yes | 2016-06-04 // 4 match keywords
5 | yesyes3 | yes yes yes yes | 2016-06-05 // 6 match keywords
现在需要用 count_match 列显示它
id | title | msg |date |count_match
---+-----------+-------------------+------------+-------------------
1 | test1 | yes | 2016-06-01 | 2
2 | yes1 | no | 2016-06-02 | 1
3 | test2 | no | 2016-06-03 | 1
4 | yes2 | yes yes yes | 2016-06-04 | 4
5 | yesyes3 | yes yes yes yes | 2016-06-05 | 6
对于数组,它应该显示如下
array (
[0] => array (
[id] => 5
[title] => yesyes3
[msg] => yes yes yes yes
[date] => 2016-06-05
[match] => 6
)
[1] => array (
[id] => 4
[title] => yes2
[msg] => yes yes yes
[date] => 2016-06-04
[match] => 4
)
[2] => array (
[id] => 1
[title] => test1
[msg] => yes
[date] => 2016-06-01
[match] => 2
)
[3] => array (
[id] => 3
[title] => test2
[msg] => no
[date] => 2016-06-03
[match] => 1
)
[4] => array (
[id] => 2
[title] => yes1
[msg] => no
[date] => 2016-06-02
[match] => 1
)
)
目前我为此获取的代码是这样的
$match = array('test','yes');
$orderbyString1 = "";
$orderbyString = "";
$likestr = "";
foreach($match AS $value)
{
$orderbyString .= "IF(m.title LIKE '%".$value."%' OR m.msg LIKE '%".$value."%',1,0)+";
$likestr .= "m.title LIKE '%".$value."%' OR m.msg LIKE '%".$value."%' OR ";
}
$orderbyString = substr($orderbyString, 0, -1);
$likestr = substr($likestr, 0, -4);
$this->db->select('m.*, ('.$orderbyString.') as count_match');
$this->db->from('messages m');
$this->db->where($likestr." ORDER BY ".$orderbyString." DESC");
$query = $this->db->get();
$results = $query->result_array();
print_r($results);exit;
有什么方法可以让 select 使用 php codeigniter 代码显示正确的 count_match 吗?因为目前它在 count_match.
下显示 1 和 2谢谢!
在 php 方面,有很多选项可以计算数组中的关键字。如果您需要其他功能,例如无大小写匹配或 word boundaries how about using regex.
一个想法preg_match_all
Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.
$pattern = '~(?:yes|test)~i';
foreach($arr AS $k => $v)
$arr[$k]['match'] = preg_match_all($pattern, $v['title']." ".$v['msg']);
模式只是一个 alternation of the two keywords using a non-capturing group. After the closing pattern delimiter ~
used the i
flag for caseless matching. Regex101 是测试模式的好地方。
如果输入是通用的,请使用 preg_quote
从其特殊的正则表达式含义中转义某些字符。