douzhang2680 2013-07-01 18:21
浏览 92
已采纳

拆分字符串以放入CodeIgniter Active Record SQL查询

I am trying to do a search query to see if a textarea contains some keywords. I'm having a bit of trouble though plugging my textarea's values into the query.

This is my query:

$match = $this->input->post('wStory');
$this->db->where("`terms` LIKE '%$match%'");
$query = $this->db->get('filter_tbl');
return $query->num_rows();

The $match is my text field and what I have been trying to do is split up the words inside and then run through each individual word. I have tried using PHPs' explode() function which sort of works, but in this case it doesn't work because it turns the string into an array.

Is there any way I can split up the strings in my textarea to just words and run through the words in the like statement, Or is there just something I'm missing?

  • 写回答

4条回答 默认 最新

  • drg14799 2013-07-01 18:47
    关注

    The query you're currently running is doing a check for a specific phrase, rather than checking for any of the specified words.

    You need to do the following:

    $match = $this->input->post('wStory');
    
    // break search phrase into keywords
    $keywords = explode(' ', $match);
    
    // Build query
    foreach ($keywords as $keyword)
    {
        $keyword = trim($keyword);
        $this->db->or_where("`terms` LIKE '%$keyword%'");
    }
    $query = $this->db->get('filter_tbl');
    return $query->num_rows();
    

    The explode(' ', $match) does not take into account any punctuation that might separate words.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序