Last active
November 7, 2016 12:24
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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