dongyi1111 2012-05-01 17:03
浏览 29
已采纳

CodeIgniter - 搜索结果 - 如何创建好的查询和URI?

I'm working on some script and I would like to create search. My project is based on CodeIgniter and my desire is to have all code content compatibile with it.

So, I have already working on the query for searching but it's not good because it doesn't support more words than one. So if I enter word "test" in my search form (assume that the test word is in database in one of fields) there will be few results, but if I enter words "test test test" (again assume that the test words are in database in one of fields) there will be no any result.

My current query:

$this->db->select('title, content, date, hyperlink')->or_like(array('title' => $query, 'content' => $query))->order_by('id_article', 'desc')->get('news');

The next problem is with URI because if I search for "some article, today is sunny" there is problem with comma and in other cases probably with other disallowed characters too. I have read few other question there, but all that I found is $this->uri->uri_to_assoc(), which I think in my case wouldn't work, because I don't use English in my project.

What's the best solution for these two problems?

  • 写回答

1条回答 默认 最新

  • dongxianghui3709 2012-05-01 18:09
    关注

    To solve the search issue you should probably explode the query into pieces.

    $query = 'test test 2 test3';
    $parts = explode(' ', $query);
    

    Then loop through the parts and add or_like conditions.

    foreach ($parts as $q)
    {
        $this->db->or_like(array('title' => $q, 'content' => $q));
    }
    

    Now it checks for any of the words.

    You may also want to allow quoted values to be treated as one word so that "test 2" would search for the sequence test 2 and not just test and 2. You would need to adjust how you get parts, maybe by a regular expression like:

    preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $query, $parts);
    

    As for the invalid characters in the URI, pass the string through urlencode on one end then urldecode on the other.

    $uri = urlencode('some article, today is sunny');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题