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.

Revisions

  1. iamswapnilsonar revised this gist Nov 7, 2016. No changes.
  2. iamswapnilsonar created this gist Nov 7, 2016.
    16 changes: 16 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    <?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);
    ?>