如何对数组进行分页

时间:2016-07-04 19:01:44

标签: php cakephp pagination cakephp-3.0

CakePHP 3中的标准分页组件似乎只允许数据库查询的分页。如何调整/覆盖分页以显示数据数组的分页?

1 个答案:

答案 0 :(得分:2)

您可以使用集合来操作数组数据 http://book.cakephp.org/3.0/en/core-libraries/collections.html

  

Cake \ Collection \ Collection :: take(int $ size,int $ from)

     

每当你想使用take()函数获取一个集合时,它将创建一个新集合,其中最多包含你在第一个参数中指定的值的数量,从第二个参数中传递的位置开始:

     

$topFive = $collection->take(5); // Take 5 data from the collection starting from position 4 $nextTopFive = $collection->sortBy('age')->take(5, 4);

     

位置从零开始,因此第一个位置编号为0。