Home  >  Article  >  PHP Framework  >  ThinkPHP6 paging solution with search conditions

ThinkPHP6 paging solution with search conditions

王雪芹
王雪芹Original
2020-05-02 17:17:522533browse

In ThinkPHP6, it is not difficult to implement paging, and it is not difficult to implement search, but searching with search conditions stumps many people. In the same situation, we moved the tp5 code and found that it did not work. So how to solve the paging with search conditions in ThinkPHP6?

Let’s look at the specific scenario. I searched for a keyword and selected a category to search and filter articles.

1. Find the problem

First of all, let’s take a look at the first page with search conditions:

ThinkPHP6 paging solution with search conditions

Then let’s look at the second page:

ThinkPHP6 paging solution with search conditions

Those who are careful will find that there is no URL in the second and third pages. With search criteria.

2. How to solve it:

The key to the problem has been found, so how to solve it? Let’s first go to the official website manual and look for it:

ThinkPHP6 paging solution with search conditions

In the manual, there is a query item that is responsible for passing extra parameters in the url, so it will be easy to handle.

Look at the code directly:

$where=[];//筛选条件数组
     if(input('cate_id')){
        $where[] = [
                     ['a.cate_id', '=', $cate_id],
                 ];
     }

     if(input('searchkey')){
        $where[] = [
                     ['title', 'like', '%'.$searchkey.'%'],
                 ];
     }
     $archivesData=Db::name('archives')->alias('a')->
            field('a.id,a.title,a.listorder,b.cate_name,a.time')->
            join('category b','a.cate_id=b.id')->
            where($where)->
            order('a.listorder asc')->//小到大
            order('a.id DESC')->//大-》小
            paginate([
              'list_rows'=> 3,//每页数量
              'query' => request()->param(),
              ]);

In this way, we have perfectly solved the problem of "ThinkPHP6 paging with search conditions". In fact, don't be afraid if you encounter problems. Let's take a closer look at the manual first. Maybe it can solve the problems we encounter.

【Related recommendations】

1. 【Dry information】ThinkPHP6 docking WeChat scan code to log in

2. Using factory mode to implement Thinkphp6.0 access to Alibaba Cloud SMS

The above is the detailed content of ThinkPHP6 paging solution with search conditions. 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