Laravel 4 分页计数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18213089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 pagination count
提问by erm_durr
I have set a pagination in my specific view/site, and it works.
我在我的特定视图/站点中设置了一个分页,并且它有效。
The problem is I have a php counter:
问题是我有一个 php 计数器:
<?php $count = 0;?>
@foreach ($players as $player)
<?php $count++;?>
<tr>
<td>{{ $count }}. </td>
and whenever I switch pages, it starts from 1.
每当我切换页面时,它都是从 1 开始的。
How could I change that?
我怎么能改变呢?
回答by vFragosop
In order to achieve that, you need to initialize the value of counter:
为了实现这一点,您需要初始化计数器的值:
<?php $count = (($current_page_number - 1) * $items_per_page) + 1; ?>
NoticeI'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
.
请注意,我首先1
从当前页面中减去,因此第一个页码是0
. 然后我将添加1
到总结果中,因此您的第一个项目以1
, 而不是0
.
Laravel Paginator provides a handy shortcut for that:
Laravel 分页器为此提供了一个方便的快捷方式:
<?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();
回答by Ian Romie Ona
<?php echo "Displaying ".$data->getFrom() ." - ".$data->getTo(). " of ".number_format($data->getTotal())." result(s)"; ?>
回答by Ifan Iqbal
We only need method getFrom
from Paginator instance to be able counting from the first item in the page.
我们只需要getFrom
来自 Paginator 实例的方法就可以从页面中的第一项开始计数。
<?php $count = $players->getFrom(); ?>
@foreach ($players as $player)
<tr>
<td>{{ $count++ }}. </td>
</tr>
@endforeach
回答by Octavian Ruda
You don't need a counter.
你不需要柜台。
After you get the key that starts from 0, you need to add 1. After that you add the current page-1 * items per page
得到从0开始的key后,需要加1,然后加当前页-1 * 每页的items
You can do it like this:
你可以这样做:
@foreach ($players as $key => $player)
<tr>
<td>{{ $key+1+(($players->getCurrentPage()-1)*$players->getPerPage()) }}</td>
</tr>
@endforeach
回答by user4413898
Simple and elegant:
简单而优雅:
@foreach ($players as $key => $player)
<tr>
<td>{{ $players->getFrom() + $key }}</td>
</tr>
@endforeach
回答by Abishek
<?php $count++; ?>
@if($players->getCurrentPage() > 1)
{{ ((($players->getCurrentPage() - 1)* $players->getPerPage()) + $count)}}
@else
{{$count}}
@endif