title: Dynamic limit per page Knp Pagination
body: <p>I need straightforward solution for dynamically set number of records per page with Knp Pagination Bundle.</p>
  
  <p>I read the this page <a href="https://stackoverflow.com/questions/12707973/records-per-page-allow-user-to-choose-codeigniter-pagination">records per page allow user to choose - codeigniter pagination</a> about dynamically set per page limits and I know I need a drop down with hyperlink inside each item that send a request to server and server use parameter on this request to set limit per page on knp pagination bundle. But I don't know exactly how to handle this actions on server and also and more harder to me how to create items on drop down related to the total number on my query.</p>
  
  <p>my controller:</p>
  
  <pre><code>public function indexAction()
  {   
      $page_title = Util::getFormattedPageTitle("Customers");
  
      $em    = $this-&gt;get('doctrine.orm.entity_manager');
      $dql   = "SELECT a FROM CustomersBundle:Customer a WHERE a.enable = 1 ORDER BY a.id";
      //$query = $em-&gt;createQuery($dql);
      //$customers = $query-&gt;execute();
  
      $query = $em-&gt;createQuery($dql); 
      $paginator  = $this-&gt;get('knp_paginator');
      $pagination = $paginator-&gt;paginate(
          $query,
          $this-&gt;get('request')-&gt;query-&gt;get('page', 1)/*page number*/,
          3/*limit per page*/
      );
  
  
      // parameters to template
      return $this-&gt;render('CustomersBundle:Default:index.html.twig', array(
          'page_title' =&gt; $page_title,
          'pagination' =&gt; $pagination,
          'image_path' =&gt; CustomersConstants::$customers_image_thumb_path
      ));
  }
  </code></pre>
  
  <p>And my view code is:</p>
  
  <pre><code>    {% extends '::base.html.twig' %}
  
  {% block title %}
      {{ page_title }}
  {% endblock %}
  
  {% block body %}
      &lt;h1&gt;
          {{ "customers" }}
      &lt;/h1&gt;
      &lt;br /&gt;
      {% for customer in pagination %}
      &lt;a href="{{ customer.url }}" target="_blank"&gt;
          &lt;div dir="rtl" class="st_simple_box" id="customer{{customer.id}}" &gt;
              &lt;table cellpadding="0" cellspacing="0" width="100%"&gt;
                  &lt;tr&gt;
                      &lt;td style="vertical-align: top; width: 115px;"&gt;
                          {% if customer.imageName is not empty %}
                              &lt;img class="newsimage" src="{{asset(image_path) ~ customer.imageName}}" alt="No Image/&gt;
                          {% else %}
                              &lt;img class="newsimage" src="{{asset('images/vakil_default_small.png') ~ customer.imageName}}" alt="No Image"/&gt;
                          {% endif %}
                      &lt;/td&gt;
                      &lt;td style="text-align: center;"&gt;
                          &lt;p&gt;&lt;span style="font-family: Tahoma;"&gt;{{customer.titleFa}}&lt;/span&gt;&lt;/p&gt;
                      &lt;/td&gt;
                      &lt;td style="text-align: justify;"&gt;
                          &lt;p&gt;&lt;span style="font-family: Tahoma;"&gt;{{customer.descriptionFa}}&lt;/span&gt;&lt;/p&gt;
                      &lt;/td&gt;
                  &lt;/tr&gt;
              &lt;/table&gt;
          &lt;/div&gt;
      &lt;/a&gt;
      {% endfor %}
      &lt;div class="navigation"&gt;
          &lt;span&gt;
          {{ knp_pagination_render(pagination) }}
          &lt;/span&gt;
          &lt;!--Here is my drop down html code--&gt;
      &lt;/div&gt;
      &lt;br /&gt;
  
  {% endblock %}
  </code></pre>
  
  <p>**</p>
  
  <h2>Edited on 12 March 2014</h2>
  
  <pre><code>   ||||
     ||||
     ||||
    \\\///
     \\//
      \/
  </code></pre>
  
  <p>Is there any way to set MaxItemPerPage on cookie to have a integrated variable and use this variable or change it while Knp Pagination is showed up on twig file.</p>
  
  <p>the answer was correct but because of I used pagination in many bundles to paginate between my entities so I need an integrate variable to change all of them. I use your answer and customize sliding.html.twig on Knp Pagination to reach this code for "customized_sliding.html.twig"</p>
  
  <pre><code>&lt;div class="ui small pagination menu"&gt;
      &lt;div style="clear: both;"&gt;
          {% if pageCount &gt; 1 %}
              &lt;div class="pagination" style="float: right;"&gt;
                  {% if first is defined and current != first %}
                      &lt;a href="{{ path(route, query|merge({(pageParameterName): first})) }}" class="small icon item"&gt;
                          &lt;i class="small double angle right icon"&gt;&lt;/i&gt;
                      &lt;/a&gt;
                  {% endif %}
  
                  {% if previous is defined %}
                      &lt;a href="{{ path(route, query|merge({(pageParameterName): previous})) }}" class="small icon item"&gt;
                          &lt;i class="small angle right icon"&gt;&lt;/i&gt;
                      &lt;/a&gt;
                  {% endif %}
  
                  {% for page in pagesInRange %}
                      {% if page != current %}
                          &lt;a href="{{ path(route, query|merge({(pageParameterName): page})) }}" class="small item"&gt;
                              {{ page }}
                          &lt;/a&gt;
                      {% else %}
                          &lt;a class="small active item"&gt;
                              {{ page }}
                          &lt;/a&gt;
                      {% endif %}
                  {% endfor %}
  
                  {% if next is defined %}
                      &lt;a href="{{ path(route, query|merge({(pageParameterName): next})) }}" class="small icon item"&gt;
                          &lt;i class="small angle left icon"&gt;&lt;/i&gt;
                      &lt;/a&gt;
                  {% endif %}
  
                  {% if last is defined and current != last %}
                      &lt;a href="{{ path(route, query|merge({(pageParameterName): last})) }}" class="small icon item"&gt;
                          &lt;i class="small double angle left icon"&gt;&lt;/i&gt;
                      &lt;/a&gt;
                  {% endif %}
              &lt;/div&gt;
          {% endif %}
          &lt;div style="float: left;"&gt;
              &lt;select name="maxItemPerPage" id="maxItemPerPage"&gt;
                  &lt;option selected="true" style="display:none;"&gt;Number Per Page&lt;/option&gt;
                  &lt;option id="10"&gt;5&lt;/option&gt;
                  &lt;option id="20"&gt;10&lt;/option&gt;
                  &lt;option id="30"&gt;20&lt;/option&gt;
              &lt;/select&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;script type="text/javascript"&gt;
          //on select change, you navigate to indexAction and send the parameter maxItemPerPage
          $('#maxItemPerPage').change(function(){
              {% set currentPath = path(app.request.attributes.get('_route')) %}
              var url = "{{path(app.request.attributes.get('_route'),{'maxItemPerPage': '_itemNum'})}}";
              var item = $('#maxItemPerPage').find(":selected").text();
              jQuery(location).attr('href', url.replace('_itemNum',item ));
          });
      &lt;/script&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I want to fetch and store MaxItemPerPage from cookie so there is no need to change all bundles code.
  For example in my controller I don't know have to fetch the $maxItemPerPage from cookie</p>
  
  <pre><code>$pagination = $paginator-&gt;paginate(
              $query,
              $this-&gt;get('request')-&gt;query-&gt;get('page', 1)/*page number*/,
              $maxItemPerPage/*limit per page*/
          );
  </code></pre>
  
  <p>And I need change the value of maxItemPerPage on cookie by changing the value of html  tag by javascript and redirect the page to same controller and no need to send maxItemPerPage to controller.</p>