Home>Article>PHP Framework> Create paginated data using arrays

Create paginated data using arrays

藏色散人
藏色散人 forward
2019-09-18 13:57:10 3107browse

Create paginated data using arrays

Instructions

Using version: 5.1.35 LTS

A scenario encountered is , the data is an array obtained from the RPC remote call interface, and needs to be displayed on the front-end page.

Solution

You can use the make method of the think\Paginate class to create paginated data. Make method prototype:

/** * @access public * @param $items 需要分页的数据 * @param $listRows 每页数据条数 * @param null $currentPage 当前页数 * @param null $total 总页数 * @param bool $simple 是否使用简单模式(只有上一页和下一页) * @param array $options 其他参数选项,如查询参数,url路径等 * @return Paginator 返回一个分页对象 */ public static function make($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) { return new static($items, $listRows, $currentPage, $total, $simple, $options); }

See the code comments for the parameters that need to be passed in.

Since the think\Paginate class is an abstract class, another class needs to inherit it to use its public methods. The think\paginator\driver\Bootstrap class in the framework inherits it, so you can use this class to call the make method.

So, you can write a method to create pagination data from an array, which probably looks like this:

private function getPaginateData($data, $page, $query){ return Bootstrap::make($data, $perPage, $page, $total, false, ['path' => url('module/controller/action'), 'query' => $query]); }

After using this method to generate a paging object, for example: $data = $this->getPaginateData (...), output it to the template in the controller, and then add it to the template page:

{$data|raw}

The template engine will automatically render the paging style.

Recommended tutorial:thinkphp tutorial

The above is the detailed content of Create paginated data using arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete