1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php

$data = array();

for($i = 1; $i <= 100; $i++) {
    $oneData = array();
    $oneData["item_id"] = $i;
    $oneData["item"] = "Item ".$i;
    $oneData["item_cd"] = "Item code ".$i;
    $data[] = $oneData;
}

$page = $_GET['page'];
$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];

if(!$sidx) $sidx = 1;

$count = count($data);

if($count > 0) {
    $total_pages = ceil($count/$limit);
} else {
    $total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;

$start = $limit*$page - $limit;

$result["page"] = $page;
$result["total"] = $total_pages;
$result["records"] = $count;

$i=0;
for($j = $start; $j < $start + $limit; $j++) {
    $row = $data[$j];
    $result["rows"][$i]['id'] = $row["item_id"];
    $result["rows"][$i]['cell'] = array($row["item_id"],$row["item"],$row["item_cd"]);
    $i++;
}
echo json_encode($result);