我从外部源检索我的数据,因此必须手动构建分页。然而,它只输出所有结果,起初我认为这可能是因为我的数组内部有对象,但在两种情况下都会出现。我做错了什么?
<?php
class MainController extends BaseController {
// Base controller specifies layout
// protected $layout = 'master';
public function index()
{
$this->layout->title = 'testing';
$this->layout->main = View::make('testing');
$objArray = array();
$entry = new \stdClass();
$entry->title = "testing";
$objArray[] = $entry;
$objArray[] = $entry;
$objArray[] = $entry;
$objArray[] = $entry;
$this->layout->main->paginated = Paginator::make($objArray, count($objArray), '2');
// var_dump($paginated->count()); // returns 4
$array = array(
array('title' => 'test'),
array('title' => 'test'),
array('title' => 'test'),
array('title' => 'test')
);
$this->layout->main->arrayPaginated = Paginator::make($array, count($array), '2');
// var_dump($arrayPaginated->count()); // returns 4
}
}
查看档案
## view/testing
@foreach($paginated as $entry)
{{$entry->title}}
@endforeach
{{$paginated->links()}} //outputs "testing" four times
@foreach($arrayPaginated as $entry)
{{$entry['title']}}
@endforeach
{{$arrayPaginated->links()}} //outputs "test" four times
答案 0 :(得分:0)
正如你在评论中指出的那样,你必须自己切片,有一点我无法理解的是第三个参数的用户是什么“每页的项目数”如果你必须限制的话将条目传递给分页器之前的条目。
$objArray = array();
for($i = 0; $i < 4; $i++) {
$entry = new \stdClass();
$entry->title = "test {$i}";
$objArray[] = $entry;
}
$perPage = 2;
$page = Input::get('page', 1); // Set default to 1 if nothing is passed
// ($page - 1) so that when page is 1 you are targetting the start of the array
$pagedData = array_slice($objArray, ($page - 1) * $perPage, $perPage);
$this->layout->main->paginated = Paginator::make($pagedData, count($objArray), $perPage);
视图文件中没有任何变化。