Tdt\Core\Pager::calculatePagingHeaders PHP Method

calculatePagingHeaders() public static method

Calculate the link meta-data for paging purposes, return an array with paging information
public static calculatePagingHeaders ( integer $limit, integer $offset, integer $total_rows ) : array
$limit integer
$offset integer
$total_rows integer The total amount of objects
return array
    public static function calculatePagingHeaders($limit, $offset, $total_rows)
    {
        $paging = array();
        // Check if limit and offset are integers
        if (!is_integer((int) $limit) || !is_integer((int) $offset)) {
            \App::abort(400, "Please make sure limit and offset are integers.");
        }
        // Calculate the paging parameters and pass them with the data object
        if ($offset + $limit < $total_rows) {
            $paging['next'] = array($limit + $offset, (int) $limit);
            $last_page = ceil($total_rows / $limit);
            $paging['last'] = array($last_page * $limit, (int) $limit);
        }
        if ($offset > 0 && $total_rows > 0) {
            $previous = $offset - $limit;
            if ($previous < 0) {
                $previous = 0;
            }
            $paging['previous'] = array($previous, $limit);
        }
        return $paging;
    }

Usage Example

Ejemplo n.º 1
0
 public function readData($source_definition, $rest_parameters = [])
 {
     list($limit, $offset) = Pager::calculateLimitAndOffset();
     $collection = $this->getCollection($source_definition);
     // Parse the parameters from the query string (prefixed by q.)
     $all_parameters = \Input::get();
     $query = [];
     foreach ($all_parameters as $key => $val) {
         if (substr($key, 0, 2) == 'q_') {
             $key = str_replace('q_', '', $key);
             $query[$key] = $val;
         }
     }
     $total_objects = $collection->count($query);
     $cursor = $collection->find($query)->skip($offset)->limit($limit);
     $results = [];
     foreach ($cursor as $result) {
         unset($result['_id']);
         $results[] = $result;
     }
     $paging = Pager::calculatePagingHeaders($limit, $offset, $total_objects);
     $data_result = new Data();
     $data_result->data = $results;
     $data_result->paging = $paging;
     $data_result->preferred_formats = $this->getPreferredFormats();
     return $data_result;
 }
All Usage Examples Of Tdt\Core\Pager::calculatePagingHeaders