Laravel 4分页计数 [英] Laravel 4 pagination count
本文介绍了Laravel 4分页计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在我的特定视图/站点中设置了分页,并且可以使用.
I have set a pagination in my specific view/site, and it works.
问题是我有一个php计数器:
The problem is I have a php counter:
<?php $count = 0;?>
@foreach ($players as $player)
<?php $count++;?>
<tr>
<td>{{ $count }}. </td>
每当我切换页面时,它都从1开始.
and whenever I switch pages, it starts from 1.
我该如何更改?
推荐答案
为此,您需要初始化counter的值:
In order to achieve that, you need to initialize the value of counter:
<?php $count = (($current_page_number - 1) * $items_per_page) + 1; ?>
通知:我首先要从当前页面中减去1
,因此第一页编号是0
.然后,我将1
添加到总结果中,因此您的第一项以1
开头,而不是0
.
Notice I'm first subtracting 1
from current page, so the first page number is 0
. Then I'm adding 1
to the total result, so your first item starts with 1
, instead of 0
.
Laravel分页器为此提供了一个方便的快捷方式:
Laravel Paginator provides a handy shortcut for that:
<?php $count = $players->getFrom() + 1; ?>
@foreach ($players as $player)
...
您可以根据需要使用其他一些内容:
There are a few others that you can use as you like:
$players->getCurrentPage();
$players->getLastPage();
$players->getPerPage();
$players->getTotal();
$players->getFrom();
$players->getTo();
这篇关于Laravel 4分页计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文