Larvel 4 pagination count

I set the pagination in my specific view / site and it works.

The problem is that I have a php counter:

<?php $count = 0;?> @foreach ($players as $player) <?php $count++;?> <tr> <td>{{ $count }}. </td> 

and whenever I switch pages, it starts with 1.

How can i change this?

+4
source share
6 answers

To do this, you need to initialize the counter value:

 <?php $count = (($current_page_number - 1) * $items_per_page) + 1; ?> 

Note I first subtract 1 from the current page, so the first page number is 0 . Then I add 1 to the overall result, so your first element starts with 1 , not 0 .

Laravel Paginator provides a convenient shortcut for this:

 <?php $count = $players->getFrom() + 1; ?> @foreach ($players as $player) ... 

There are several others you can use as you wish:

 $players->getCurrentPage(); $players->getLastPage(); $players->getPerPage(); $players->getTotal(); $players->getFrom(); $players->getTo(); 
+16
source
 <?php echo "Displaying ".$data->getFrom() ." - ".$data->getTo(). " of ".number_format($data->getTotal())." result(s)"; ?> 
+1
source

We only need the getFrom method from the getFrom instance so that it can count from the first element on the page.

 <?php $count = $players->getFrom(); ?> @foreach ($players as $player) <tr> <td>{{ $count++ }}. </td> </tr> @endforeach 
+1
source
 <?php $count++; ?> @if($players->getCurrentPage() > 1) {{ ((($players->getCurrentPage() - 1)* $players->getPerPage()) + $count)}} @else {{$count}} @endif 
0
source

You do not need a counter.

After you get the key starting with 0, you need to add 1. After that, you add the current page-1 * to the page

You can do it as follows:

 @foreach ($players as $key => $player) <tr> <td>{{ $key+1+(($players->getCurrentPage()-1)*$players->getPerPage()) }}</td> </tr> @endforeach 
0
source

Simple and elegant:

 @foreach ($players as $key => $player) <tr> <td>{{ $players->getFrom() + $key }}</td> </tr> @endforeach 
0
source

Source: https://habr.com/ru/post/1496834/


All Articles