Advertisement
cdsatrian

Lampu

Sep 6th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.55 KB | None | 0 0
  1. <?php
  2. /*********************************  
  3. FILENAME  : lampu.php  
  4. CREATE BY : cahya dsn  
  5. PURPOSE   : show lamps status
  6. CREATE DATE : 2013-09-06  
  7. **********************************  
  8. #table creation  
  9.  
  10. USE `test`;
  11.  
  12. DROP TABLE IF EXISTS `tbl_pengguna`;
  13. CREATE TABLE IF NOT EXISTS `tbl_pengguna` (
  14.   `id_pengguna` int(11) unsigned NOT NULL AUTO_INCREMENT,
  15.   `nama_pengguna` varchar(30) NOT NULL,
  16.   PRIMARY KEY(`id_pengguna`)
  17. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
  18.  
  19. INSERT INTO tbl_pengguna(nama_pengguna)
  20. VALUES
  21. ('Agus'),('Budi'),('Citra'),('Dewi'),('Erwin');
  22.  
  23. DROP TABLE IF EXISTS `tbl_lampu`;
  24. CREATE TABLE IF NOT EXISTS `tbl_lampu` (
  25.   `id_lampu` tinyint(2) unsigned NOT NULL AUTO_INCREMENT,
  26.   `nama_lampu` varchar(10) NOT NULL,
  27.   PRIMARY KEY(`id_lampu`)
  28. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
  29.  
  30. INSERT INTO tbl_lampu(nama_lampu)
  31. VALUES
  32. ('Lampu 0'),('Lampu 1'),('Lampu 2'),('Lampu 3'),('Lampu 4');
  33.  
  34.  
  35. DROP TABLE IF EXISTS `tbl_status_lampu`;
  36. CREATE TABLE IF NOT EXISTS `tbl_status_lampu` (
  37.   `id_pengguna` int(11) unsigned NOT NULL,
  38.   `id_lampu` tinyint(2) unsigned NOT NULL,
  39.   `status` SET('0','1') NOT NULL DEFAULT '0'
  40. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;
  41.  
  42. INSERT INTO tbl_status_lampu
  43. VALUES
  44. (1,1,'1'),(1,2,'0'),(1,3,'1'),(1,4,'1'),
  45. (2,1,'1'),(2,2,'0'),(2,3,'1'),(2,4,'0'),
  46. (3,1,'1'),(3,2,'0'),
  47. (4,1,'1'),(4,2,'1'),(4,3,'1'),
  48. (5,1,'0'),(5,3,'1'),(5,4,'1');
  49. */
  50. ?>
  51. <!DOCTYPE hmtl>  
  52. <html>  
  53.   <head>  
  54.     <title>STATUS LAMPU</title>  
  55.   </head>  
  56.   <body>
  57. <?php    
  58. $dbhost='localhost';
  59. $dbuser='root';
  60. $dbpass='';
  61. $dbname='test';
  62. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  63. $pengguna=isset($_GET['id'])?$_GET['id']:'';
  64. if(empty($pengguna))  
  65. {
  66.   $sql="SELECT * FROM tbl_pengguna";  
  67.   if($result=$db->query($sql))  
  68.   {  
  69.     echo "Pilih Pengguna:<ul>";  
  70.     while($record=$result->fetch_object())  
  71.     {  
  72.       echo "<li><a href='?id=".$record->id_pengguna."'>".$record->nama_pengguna."</a></li>";  
  73.     }  
  74.     echo "</ul>";  
  75.   }  
  76. }
  77. else
  78. {
  79.   $sql="SELECT a.status,b.nama_lampu
  80.        FROM tbl_status_lampu a
  81.        JOIN tbl_lampu b USING(id_lampu)
  82.        WHERE a.id_pengguna='$pengguna'";
  83.   if($result=$db->query($sql))
  84.   {
  85.     echo "<table>"
  86.         ."<tr>"
  87.         ."<th>id pengguna</th>"
  88.         ."<td>$pengguna</td>"
  89.         ."</tr>";
  90.     while($record=$result->fetch_object())  
  91.     {
  92.       echo "<tr>"
  93.           ."<th>".$record->nama_lampu."</th>"
  94.           ."<td>".($record->status=='1'?'nyala':'mati')."</td>"
  95.           ."</tr>";
  96.     }
  97.     echo "</table>";
  98.   }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement