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条)

报告相同问题?

悬赏问题

  • ¥20 阿里云python代码求解
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路