Skip to content

Instantly share code, notes, and snippets.

@iamswapnilsonar
Last active November 7, 2016 12:24
Show Gist options
  • Save iamswapnilsonar/d3dd84b169afb631c07a1355eb58d0c8 to your computer and use it in GitHub Desktop.
Save iamswapnilsonar/d3dd84b169afb631c07a1355eb58d0c8 to your computer and use it in GitHub Desktop.
If you want to create simple pagination example in PHP, you could do something like this.
<?php
//Swapnil_Sonar workspace
$sample_data = '[{"id":1,"name":"Item1","type":"Android"},{"id":2,"name":"Item2","type":"Android"},{"id":3,"name":"Item3","type":"iOS"},{"id":4,"name":"Item4","type":"Windows"},{"id":5,"name":"Item5","type":"iOS"},{"id":6,"name":"Item6","type":"Android"},{"id":7,"name":"Item7","type":"Windows"}]';
// just normal getting data
$raw_data = json_decode($sample_data, true);
// use get variable to paging number
$page = 2; //!isset($_GET['page']) ? 1 : $_GET['page'];
$limit = 5; // five rows per page
$offset = ($page - 1) * $limit; // offset
$total_items = count($raw_data); // total items
$total_pages = ceil($total_items / $limit);
$final = array_splice($raw_data, $offset, $limit); // splice them according to offset and limit
echo json_encode($final);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment