thinkphp5.0+ 下的分頁方法,文件地址是:thinkphp\thinkphp\library\think\db\Query.php
/**
* 分頁查詢
* @param int|null $listRows 每頁數量
* @param int|bool $simple 簡潔模式或者總記錄數
* @param array $config 配置參數
* page:當前頁,
* path:url路徑,
* query:url額外參數,
* fragment:url錨點,
* var_page:分頁變量,
* list_rows:每頁數量
* type:分頁類名
* @return \think\paginator\Collection
* @throws DbException
*/
public function paginate($listRows = null, $simple = false, $config = [])
{
if (is_int($simple)) {
$total = $simple;
$simple = false;
}
$config = array_merge(Config::get('paginate'), $config);
$listRows = $listRows ?: $config['list_rows'];
/** @var Paginator $class */
$class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']);
$page = isset($config['page']) ? (int) $config['page'] : call_user_func([
$class,
'getCurrentPage',
], $config['var_page']);
$page = $page < 1 ? 1 : $page;
$config['path'] = isset($config['path']) ? $config['path'] : call_user_func([$class, 'getCurrentPath']);
if (!isset($total) && !$simple) {
$options = $this->getOptions();
$bind = $this->bind;
$total = $this->count();
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select();
} elseif ($simple) {
$results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select();
$total = null;
} else {
$results = $this->page($page, $listRows)->select();
}
return $class::make($results, $listRows, $page, $total, $simple, $config);
}
很多時候我們都需要根據條件查詢數據,直接get提交查詢的話就會生成所謂的叫做普通模式路由模式,然鵝我們使用paginate這個方法只傳第一個參數的時候,點擊分頁發現搜索的參數並沒有帶上,所以我們就要用到後面的參數了,第二個參數
simple:簡潔模式或者總記錄數—−這裏不討論,第三個參數 config : 配置參數,
page:當前頁,
path:url路徑,
query:url額外參數,
fragment:url錨點,
var_page:分頁變量,
list_rows:每頁數量
type:分頁類名
這裏有一個 query:url額外參數,,我們加上這個配置參數就可以把搜索的條件帶上了。
前面省略 ->paginate(20, false, [
'query' => input('param.'),
]);