title: How to make Select2 infitine scroll pagination on server side and client side
body: <p>On documentation page there is no server side explanation. As Im confused, I want to solve my problem and maybe make a reference with server side and client side code.</p>
  
  <p>I'm trying to make select2 (using v.4.0) infinite scroll pagination as a searchbox, but confused with <code>params.page</code>. How can I get that parameter as query request and return the page number for infinite scroll. How infinite scroll triggers? Below are my codes which I can only get 10 results.</p>
  
  <p>HTML part of my code is;</p>
  
  <pre><code>&lt;select id="search_products" name="q"&gt;&lt;/select&gt;
  </code></pre>
  
  <p>JS part is;</p>
  
  <pre><code>jQuery('#search_products').select2({        
      ajax: {
          url: "search_results.php",
          dataType: 'json',
          delay: 250,
          data: function (params) {
            return {
              q: params.term, // search term
              page_limit: 10,
              page:params.page
            };
          },
  
          processResults: function (data, params) {
  
          params.page = params.page || 1;
  
          return {
            results: data.items,
            pagination: {
              more: (params.page * 30) &lt; data.total           
            }
          };
  
        },            
          cache: true
        },
        escapeMarkup: function (markup) { return markup; },
        minimumInputLength: 3,
        templateResult: formatRepo,
        templateSelection: formatRepoSelection
  });
  
  function formatRepo (repo) {
      if (repo.loading) return repo.adi;
  
      var markup = '&lt;div class="clearfix"&gt;' +        
      '&lt;div class="col-sm-7"&gt;' + repo.name + '&lt;/div&gt;' +
      '&lt;div class="col-sm-4"&gt;&lt;i class="fa fa-barcode"&gt;&lt;/i&gt; ' + repo.barkod + '&lt;/div&gt;';
  
      return markup;
    }
  
    function formatRepoSelection (repo) {
      return repo.adi;
    }
  </code></pre>
  
  <p>Server side - PHP part (<code>search_results.php</code>) is:</p>
  
  <pre><code>if (isset($_REQUEST['q']) &amp;&amp; !empty($_REQUEST['q'])) {
  
      $q = getVar('q', 'anp'); //getVar is my custom sanitation function
      $pglm = getVar('page_limit', 'int');
      $page_lim = (empty($pglm) ? 10 : $pglm);
      $pg = getVar('page', 'int');
      $page =  (empty($pg) ? 0 : $pg);
      $sira = $page*$page_lim;
  
      //using MYSQL, querying with custom database query class $db
      $qu = $db-&gt;get_results("
                      SELECT * FROM  `products` 
                      WHERE  `adi` LIKE  '%$q%'
                      OR  `ozellikleri` LIKE  '%$q%'
                      OR  `ozet` LIKE  '%$q%'
                      OR  `barkod` LIKE  '%$q%' LIMIT $sira, $page_lim
                      ");
  
      if($qu &amp;&amp; count($qu) &gt; 0) {
          foreach ($qu as $oqu) {
              $items[] = array('name' =&gt; $oqu-&gt;adi, 'id' =&gt; $oqu-&gt;id,  'barkod' =&gt; $oqu-&gt;barkod);
              } 
  
          echo json_encode(array('items' =&gt; $items, 'total' =&gt;  count($qu), 'page' =&gt; $page));
      }   
  }
  </code></pre>