Technologies being used:
PHP 5.6
Laravel Framework 5.3 (with Blade templating)
ElasticSearch 2.4.1
Bootstrap (for CSS pagination classes)
<ul class = "pagination pagination-lg">
@if($search_results['hits']['total'] != 0)
{{-- $search_results['hits']['total']/10) + 2) // the total amount of hits + 2 for the « and » options. --}}
@for($x = 0; $x < (ceil($search_results['hits']['total']/10) + 2); $x++)
@if($x == 0)
<li><a href = "/search/?search_query={{$query . "&size=10" . "&from=". $x * 10}}">«</a></li>
@elseif($x == $current_page)
<li class="active"><a href="#">{{$x}}</a></li>
@elseif($x == (ceil($search_results['hits']['total']/10) + 1))
<li><a href = "/search/?search_query={{$query . "&size=10" . "&from=". ($search_results['hits']['total'] - ($search_results['hits']['total'] % 10))}}">»</a></li>
@else
<li><a href = "/search/?search_query={{$query . "&size=10" . "&from=". ($x-1) * 10}}">{{$x}}</a></li>
@endif
@endfor
@endif
</ul>
This is my first go at pagination. Using a small sample of data, it seems to work - however I'm not sure if this is a good way to achieve pagination. Since the results are from ElasticSearch and not Laravel models, I don't think I can use Laravel's built in pagination.
For simplicity I'm currently returning 10 results per page - however I'm not planning on keeping this value hardcoded.
How can I make this code better? What flaws do you see in it?
$search_results
array. – Mike Brant Nov 14 '16 at 15:09