-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.php
More file actions
54 lines (48 loc) · 1.23 KB
/
Copy pathserver.php
File metadata and controls
54 lines (48 loc) · 1.23 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
<?php
// sort helpers
function sortme_desc($a, $b) {
if ($a->sort == $b->sort) {
return 0;
}
return ($a->sort < $b->sort) ? -1 : 1;
}
function sortme_asc($a, $b) {
if ($a->sort == $b->sort) {
return 0;
}
return ($a->sort > $b->sort) ? -1 : 1;
}
// default values
$page = !empty($_GET['page']) ? $_GET['page'] : 1;
$limit = !empty($_GET['limit']) ? $_GET['limit'] : 10;
$sort = !empty($_GET['sort']) ? $_GET['sort'] : 'id';
$order = !empty($_GET['order']) ? $_GET['order'] : 'desc';
// load sample data
$data = json_decode(file_get_contents('example.json'));
// sort data
foreach( $data as $k => $v) {
$data[$k]->sort = $v->$sort;
}
usort( $data, 'sortme_' . $order );
// limit data
$data = array_chunk($data, $limit);
$data = $data[$page-1];
// build response and add controls
$out = array('success' => true, 'data' => array());
foreach( $data as $k => $v ) {
unset($v->sort);
$out['data'][$k]['cols'] = $v;
$out['data'][$k]['ctrl'] = array(
'edit' => array(
'url' => '#edit',
'label' => 'Edit',
'img' => 'images/edit.png',
'css' => 'edit-btn'),
'delete' => array(
'url' => '#delete',
'label' => 'Delete',
'img' => 'images/delete.png',
'css' => 'delete-btn')
);
}
echo json_encode($out);