Advertisement
Guest User

Untitled

a guest
Mar 29th, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.64 KB | None | 0 0
  1. <?php
  2. switch ($this->getLoadType()) {
  3.     case self::LOAD_TYPE_PAGINATION:
  4.         // Create the paginator as you wish.
  5.         // Because I'm using Doctrine, I have a $query variable and pass it
  6.         // to the Doctrine's paginator adapter for Zend\Paginator.
  7.         $paginator;
  8.  
  9.         $items = $paginator->getCurrentItems();
  10.         break;
  11.  
  12.     case self::LOAD_TYPE_INFINITE_SCROLL:
  13.         // Items per page
  14.         $itemCount = 24;
  15.  
  16.         // How many portions have been loaded so far.
  17.         // By default 1 portion has been loaded.
  18.         $portions = $queryParams['portions'] ?: 1;
  19.  
  20.         // Which portion to load (used in AJAX requests).
  21.         // Works like some kind of an offset.
  22.         $portion = $queryParams['portion'];
  23.  
  24.         // Let's calculate the limit from the portions that have to be loaded
  25.         // times the item count per page (per scroll).
  26.         $limit = $portions * $itemCount;
  27.         $offset = 0;
  28.  
  29.         // If we are loading a specific portion (via AJAX)
  30.         // the portion query param will be specified.
  31.         if ($portion) {
  32.             // The limit is the item count
  33.             $limit = $itemCount;
  34.  
  35.             // The offset is the portion that has to be loaded minus one (because it works as an index)
  36.             // times the item count.
  37.             $offset = ($portion - 1) * $itemCount;
  38.         }
  39.  
  40.         // Here, I'm working with doctrine and I have a $queryBuilder variable,
  41.         // so I just pass the $limit and $offset to it, but you could pass them to
  42.         // any query (even to a plain old SQL)
  43.         $items = $queryBuilder
  44.             ->setMaxResults($limit)
  45.             ->setFirstResult($offset)
  46.             ->getQuery()
  47.             ->getResult();
  48.         break;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement