We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to deal with paginator in the searching page?

The searching page has two or more fields like keyword and source:

<form class="navbar-form pull-left" action="/articles/search" method="get">
    <input type="text" name="source" >
    <input type="text" name="keyword" >
    <button type="submit"><i class="fa fa-search"></i></button>
</form>

If we input google for source, and input usaco for keyword, and then submit it , the paginator of the result page will be more like this:

<ul class="pagination">
    <li class="active">
        <a href="/articles/search?page=1&source=google&keyword=usaco">1</a>
    </li>
    <li><a href="/articles/search?page=2&source=google&keyword=usaco">2</a></li>
    <li><a href="/articles/search?page=3&source=google&keyword=usaco">3</a></li>    
</ul>

In the controller, I cant get the pagination by $this->view->page = $paginator->getPaginate();

But how to code in the volt in order to generate the pagination HTML code above? more detailedly,

Question 1. (See the comment line in the sample code below) How to generate the URL params like &source=google&keyword=usaco?

Question 2. $this->url->get(\''articles/search?page=\") will cause XSS attack, how to avoid it?

{% if page.items is defined %}
<ul class="pagination">
    <?php for ($i=1; $i<=$page->total_pages; $i++) {
        echo "<li";
        if ($i == $page->current) echo " class='active'";
        echo "><a href='" . $this->url->get('articles/search?page=') . $i;
        // Here, How to generate the URL params like `&source=google&keyword=usaco`?
        echo "'>" . $i . "</a></li>";
    }?>
</ul>
{% endif %}
edited Aug '17

Passing the Paginator data as an object, you get the first, before, next, last variables etc. This is an example using bootstrap and font awesome, you can add your search, and keyword params accordingly.


 <div class="btn-group">
<a href="{{ url(" location") }}?page=<?= $records->first; ?>" class="btn btn-white"><i class="fa fa-chevron-left"></i><i class="fa fa-chevron-left"></i></a>
 <a href="{{ url(" location") }}?page=<?= $records->before; ?>" class="btn btn-white"><i class="fa fa-chevron-left"></i></a>
 <a href="{{ url(" location") }}?page=<?= $records->next; ?>" class="btn btn-white"><i class="fa fa-chevron-right"></i> </a>
 <a href="{{ url(" location") }}?page=<?= $records->last; ?>" class="btn btn-white"><i class="fa fa-chevron-right"></i><i class="fa fa-chevron-right"></i> </a>
 </div>
  <span class="m-l"><?php echo "Page ", $records->current, " of ", $records->total_pages; ?></span>