Old version cleaned
[weathermon.git] / web / units.php
1 <?php
2
3 function get_unit($db,$param) {
4
5   $qug = $db -> prepare(
6     'select u.id,u.unit_group from st_parameters st,units u where st.id='.$param.' and st.st_unit=u.id'
7   );
8   $qug -> execute();
9                                     
10   while ($row = $qug -> fetch(PDO::FETCH_ASSOC)) {
11     $from_unit = $row['id'];
12     $unit_group = $row['unit_group'];
13   }
14                                                                        
15   if (!empty($_COOKIE['unit_'.$unit_group])) {
16     $to_unit=intval($_COOKIE['unit_'.$unit_group]);
17   } else {
18     $to_unit=$from_unit;
19   }
20                                                                                                                 
21   $qu = $db -> prepare(
22     'select u.name_short from units u where u.id='.$to_unit
23   );
24   $qu -> execute();
25                                                                                                                                                                  
26   while ($row = $qu -> fetch(PDO::FETCH_ASSOC)) {
27     $param_unit = $row['name_short'];
28   }
29  
30   return Array(
31     'from' => $from_unit,
32     'to' => $to_unit,
33     'name' => $param_unit
34   );                                                                                                                                                                                    
35
36 }
37
38 function convert_unit($db,$value,$from,$to) {
39
40   if ($from==$to) {
41
42     $val = $value;
43
44   } else {
45
46     $qv = $db -> prepare(
47       'select round(unitconv('.$value.','.$from.','.$to.'),2) res'
48     );
49     $qv -> execute();
50                             
51     while ($row = $qv -> fetch(PDO::FETCH_ASSOC)) {
52       $val = $row['res'];
53     }
54
55   }
56                                                         
57   return $val;
58
59 }
60
61 ?>