Home  >  Article  >  PHP Framework  >  How to implement fuzzy query on multiple fields in thinkphp framework

How to implement fuzzy query on multiple fields in thinkphp framework

PHPz
PHPzOriginal
2023-04-21 10:09:511342browse

When developing web applications, it is often necessary to use fuzzy queries to implement search functions. When using the thinkphp framework, fuzzy queries can be implemented very conveniently. This article will introduce how to implement fuzzy query on multiple fields in the thinkphp framework.

First, we need to define a method in the model to obtain fuzzy query results. In the method, we can use the fuzzy query parameter % that comes with thinkphp to perform fuzzy query. The specific code is as follows:

public function getFuzzySearchResult($keyword){
    $result = $this->where('title', 'like', '%'.$keyword.'%')  //在title字段中进行模糊查询
                   ->whereOr('content', 'like', '%'.$keyword.'%')  //在content字段中进行模糊查询
                   ->select();
    return $result;
}

In the above code, we used two query conditions where and whereOr. Among them, where refers to the result of querying that the specified field is equal to the specified value, and whereOr refers to the result of querying that the specified field is equal to the specified value or the result that the specified field is equal to the specified value. Therefore, in the above code, we can perform fuzzy queries in the title and content fields and merge the result sets back.

Next, we call this method in the controller and pass the query results to the view for display. The specific code is as follows:

public function fuzzySearch(){
    $keyword = input('keyword');  //获取搜索关键字
    $model = new Article();  //实例化模型
    $result = $model->getFuzzySearchResult($keyword);  //获取模糊查询结果
    $this->assign('result', $result);  //将结果传递给视图
    return $this->fetch('search_result');  //跳转到展示页面
}

In the above code, we first obtain the search keywords and instantiate a model. Then call the getFuzzySearchResult method defined in the model to obtain the fuzzy query results. Finally, pass the results to the view and jump to the display page.

Finally, we display the fuzzy query results in the view. The specific code is as follows:

{if $result}
    {foreach $result as $item}
        <div class="article-item">
            <div class="title">{$item.title}</div>
            <div class="content">{$item.content}</div>
        </div>
    {/foreach}
{else}
    <div class="no-result">没有搜索结果哦~</div>
{/if}

In the above code, we first determine whether the search results are empty. If not, then use a foreach loop to display each result; if it is empty, the user will be prompted that there are no search results. .

Through the above method, we can implement the fuzzy query function of multiple fields in the thinkphp framework. Thank you everyone for reading, I hope it will be helpful to actual development.

The above is the detailed content of How to implement fuzzy query on multiple fields in thinkphp framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn