【问题标题】:How to find the most match keyword for search function using codeigniter php如何使用codeigniter php为搜索功能找到最匹配的关键字
【发布时间】:2016-11-04 12:46:23
【问题描述】:

这是我的表格,我需要使用 codeigniter 创建一个搜索功能,该功能将显示包含我的关键字的项目列表,按匹配关键字的最多数量和添加的最新日期排序。

例如。提交的两个关键字都是yes,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

现在它会显示预期的输出:

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('yes','test');
$array = array('m.title' => $match, 'm.msg' => $match);
$this->db->select('m.*');
$this->db->from('messages m');
$this->db->like($array);
$this->db->order_by('m.date', 'DESC');
$query = $this->db->get();

是否有任何用于此的 php codeigniter 代码?

谢谢!

【问题讨论】:

标签: php mysql arrays codeigniter search


【解决方案1】:

您需要使用搜索词创建 ORDER BY 子句。

完整代码

已编辑

$match = array('yes','test');
//$array = array('m.title' => $match, 'm.msg' => $match);//you do not need this
$orderbyString = "";
$likestr = "";
//first prepare an ORDER BY statement and like statement
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");
//$this->db->order_by($orderbyString, 'DESC');//removed this line

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

$results = $query->result_array();//contains the desired result

【讨论】:

  • 嗨@aj,谢谢,但是返回的比赛怎么样?在数组中如何显示?
  • 我收到一个错误:严重性:通知消息:数组到字符串的转换
  • 好吧,那就是$match = 'yes test;,但是foreach怎么样?它必须在数组中
  • 几乎在那里,但我得到这些错误`语法在'LIKE'%%'ESCAPE'!'附近使用按 IF 排序(`
  • 我认为使用$orderbyString的订单有问题
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
相关资源
最近更新 更多