Skip to content

Instantly share code, notes, and snippets.

@TravisBallard
Created January 25, 2016 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TravisBallard/aa41ed40fa8023e81702 to your computer and use it in GitHub Desktop.
Save TravisBallard/aa41ed40fa8023e81702 to your computer and use it in GitHub Desktop.
<?php
$total = 3270;
$perPage = 20;
$curPage = isset( $_REQUEST['page'] ) && ! empty( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1;
// set start position
$start = (($curPage * $perPage) - $perPage) + 1;
if( $start === 0 ) $start = 1;
// set end position
$end = ($start + $perPage) - 1;
if( $end > $total ) $end = $total;
// get total pages
$max = ceil( $total / $perPage );
// if current page is last, set start to last of previous page
if( $curPage == $max ){
$start = ($max - 1) * $perPage;
}
// more than max, nothing to show
if( $start > $end ){
die();
}
$arr = array(
'meta' => array(
"total_resources" => $total,
"resources_per_page" => $perPage,
"current_page" => $curPage
),
'result' => array()
);
for( $x=$start; $x<=$end; $x++){
$tmp = (object)array(
'title' => sprintf( 'Item %d', $x ),
'file_url' => null,
'node_created' => 'Thursday, September 24, 2015 - 00:00',
'nid' => $x,
'type' => 'Test',
'image' => null,
'related_industry' => null
);
array_push( $arr['result'], $tmp );
}
die(json_encode((object)$arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment