Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 &laquo; and &raquo; 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}}">&laquo;</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))}}">&raquo;</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?

share|improve this question
    
You are only showing your output template. It is very difficult to provide feedback on your overall approach to pagination without seeing how your are interacting with the data store to build your $search_results array. – Mike Brant Nov 14 '16 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.