【问题标题】:Using serial number with paginate使用带有分页的序列号
【发布时间】:2016-05-31 03:42:50
【问题描述】:

我正在尝试使用变量i 检索刀片模板中的序列号:

    <?php $i=0;?>
    @foreach ($lists as $li)
    <tr><td><?php $i++;?>{{$i}}</td><td>{{$li->Name}}</td><td>{{$li->Telephone}}
    </td><td>{{$li->mobile}}</td>
    @endforeach
    {!! $lists->render() !!}

但我在控制器中使用分页:

$lists=telephone::orderBy('name')->simplePaginate(10);

所以,第一页的序列号是可以的。但是第二页i又从1开始了。

【问题讨论】:

  • 你需要将页面大小乘以$i。

标签: php laravel laravel-5


【解决方案1】:

更新:

下面的解决方案从第一页的 0 而不是 1 开始。从 1 开始:

$i = ($lists->perPage() * ($lists->currentPage() - 1)) + 1;

尝试将$i 设置为$lists-&gt;perPage() * ($lists-&gt;currentPage() - 1)

$i = $lists->perPage() * ($lists->currentPage() - 1);

【讨论】:

  • 这是我的意思。
  • 但问题是,数字是从0而不是1开始的,怎么解决?
  • @MilanBastola 一种方法是在上述操作的结果上加 1:$i = ($lists-&gt;perPage() * ($lists-&gt;currentPage() - 1)) + 1;
  • 这个非常适合我:$i = $lists-&gt;perPage() * ($lists-&gt;currentPage() - 1) + 1;
  • 关于s.no的问题,表示从1开始到依此类推。您的解决方案只是在每一页中重复从 1 到 10 的相同计数,这不是一个正确的解决方案
【解决方案2】:

你也可以使用

@foreach($lists as $index => $list)
   {{$index + $lists->firstItem()}} //S.N
@endforeach

$index 是该结果集中的行号,$lists->firstItem() 将获得分页结果中第一项的结果号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2017-12-17
    相关资源
    最近更新 更多