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
64
65
66
67
68
69
70
|
<?php
include "db.php";
$result = $db -> query(
'SELECT machines.id as machine, IIF("values".value = 1, MIN(states.id), MAX(states.id)) as minax_state' .
' FROM machines' .
' JOIN "values" ON machines.id = "values".machine_id' .
' JOIN states ON "values".state_id = states.id' .
' WHERE "values".value IS NOT NULL' .
' GROUP BY machines.id, "values".value'
);
$stm = $db -> prepare(
'SELECT machines.name as machine, datetime(machines.last_update, "localtime") AS last_update, states.name as state, "values".value' .
' FROM machines' .
' JOIN "values" ON "values".machine_id = machines.id' .
' JOIN states ON "values".state_id = states.id' .
' WHERE machines.id = :machine' .
' AND states.id = :state'
);
$values = array();
while ($row = $result -> fetchArray()) {
$stm -> bindValue('machine', $row['machine']);
$stm -> bindValue('state', $row['minax_state']);
$stmres = $stm -> execute();
$stmres = $stmres -> fetchArray();
$values[$row['machine']][$stmres['value']] = $stmres;
$values[$row['machine']]['any'] = $stmres;
}
$stm -> close();
?>
<html><body><table>
<tr>
<th>machine</th>
<th>state</th>
<th>last update</th>
</tr>
<?php
function colorize($color, $text) {
return '<font color="' . $color . '">' . $text . '</font>';
}
foreach ($values as $value) {
if (array_key_exists('0', $value))
$color = '#800000';
else
$color = '#008000';
?> <tr>
<td><?php echo colorize($color, $value['any']['machine']); ?></td>
<td><?php
if (array_key_exists(0, $value))
echo colorize('#800000', 'not ' . $value[0]['state'] . (array_key_exists(1, $value) ? ', ' : ''));
if (array_key_exists(1, $value))
echo colorize('#008000', (array_key_exists(0, $value) ? 'but ' : '') . $value[1]['state']);
?></td>
<td><?php echo colorize($color, $value['any']['last_update']); ?></td>
</tr>
<?php
}
?>
</table></body></html>
|