-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_channel.php
63 lines (57 loc) · 1.27 KB
/
get_channel.php
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
55
56
57
58
59
60
61
62
63
<?php
$did = $_POST['id'];
$page = $_POST['page'];
$rows = $_POST['rows'];
$sort = $_POST['sort'];
$order = $_POST['order'];
if (!$page) {
$page = 1;
}
if (!$rows) {
$rows = 10;
}
$offset = ($page - 1) * $rows;
$orderby = '';
if ($sort && $order) {
$sorts = explode(',', $sort);
$orders = explode(',', $order);
$len = count($sorts);
for ($i = 0; $i < $len; $i++) {
$orderby .= "$sorts[$i] $orders[$i]";
if ($i != $len - 1) {
$orderby .= ',';
}
}
}
try {
$dbh = new PDO('mysql:host=127.0.0.1;dbname=iot0', 'hzw', '123456');
$select = 'SELECT COUNT(*) FROM channel';
$params = [];
if ($did) {
$select .= ' WHERE did = ?';
array_push($params, $did);
}
$sth = $dbh->prepare($select);
$sth->execute($params);
$result['total'] = $sth->fetch()[0];
$select = 'SELECT * FROM channel';
$params = [];
if ($did) {
$select .= ' WHERE did = ?';
array_push($params, $did);
}
if ($orderby) {
$select .= " ORDER BY $orderby";
}
$select .= " LIMIT $offset, $rows";
$sth = $dbh->prepare($select);
$sth->execute($params);
$result['rows'] = $sth->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($result);
$sth = null;
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>