Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*********************************
- FILENAME : lampu.php
- CREATE BY : cahya dsn
- PURPOSE : show lamps status
- CREATE DATE : 2013-09-06
- **********************************
- #table creation
- USE `test`;
- DROP TABLE IF EXISTS `tbl_pengguna`;
- CREATE TABLE IF NOT EXISTS `tbl_pengguna` (
- `id_pengguna` int(11) unsigned NOT NULL AUTO_INCREMENT,
- `nama_pengguna` varchar(30) NOT NULL,
- PRIMARY KEY(`id_pengguna`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- INSERT INTO tbl_pengguna(nama_pengguna)
- VALUES
- ('Agus'),('Budi'),('Citra'),('Dewi'),('Erwin');
- DROP TABLE IF EXISTS `tbl_lampu`;
- CREATE TABLE IF NOT EXISTS `tbl_lampu` (
- `id_lampu` tinyint(2) unsigned NOT NULL AUTO_INCREMENT,
- `nama_lampu` varchar(10) NOT NULL,
- PRIMARY KEY(`id_lampu`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- INSERT INTO tbl_lampu(nama_lampu)
- VALUES
- ('Lampu 0'),('Lampu 1'),('Lampu 2'),('Lampu 3'),('Lampu 4');
- DROP TABLE IF EXISTS `tbl_status_lampu`;
- CREATE TABLE IF NOT EXISTS `tbl_status_lampu` (
- `id_pengguna` int(11) unsigned NOT NULL,
- `id_lampu` tinyint(2) unsigned NOT NULL,
- `status` SET('0','1') NOT NULL DEFAULT '0'
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- INSERT INTO tbl_status_lampu
- VALUES
- (1,1,'1'),(1,2,'0'),(1,3,'1'),(1,4,'1'),
- (2,1,'1'),(2,2,'0'),(2,3,'1'),(2,4,'0'),
- (3,1,'1'),(3,2,'0'),
- (4,1,'1'),(4,2,'1'),(4,3,'1'),
- (5,1,'0'),(5,3,'1'),(5,4,'1');
- */
- ?>
- <!DOCTYPE hmtl>
- <html>
- <head>
- <title>STATUS LAMPU</title>
- </head>
- <body>
- <?php
- $dbhost='localhost';
- $dbuser='root';
- $dbpass='';
- $dbname='test';
- $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
- $pengguna=isset($_GET['id'])?$_GET['id']:'';
- if(empty($pengguna))
- {
- $sql="SELECT * FROM tbl_pengguna";
- if($result=$db->query($sql))
- {
- echo "Pilih Pengguna:<ul>";
- while($record=$result->fetch_object())
- {
- echo "<li><a href='?id=".$record->id_pengguna."'>".$record->nama_pengguna."</a></li>";
- }
- echo "</ul>";
- }
- }
- else
- {
- $sql="SELECT a.status,b.nama_lampu
- FROM tbl_status_lampu a
- JOIN tbl_lampu b USING(id_lampu)
- WHERE a.id_pengguna='$pengguna'";
- if($result=$db->query($sql))
- {
- echo "<table>"
- ."<tr>"
- ."<th>id pengguna</th>"
- ."<td>$pengguna</td>"
- ."</tr>";
- while($record=$result->fetch_object())
- {
- echo "<tr>"
- ."<th>".$record->nama_lampu."</th>"
- ."<td>".($record->status=='1'?'nyala':'mati')."</td>"
- ."</tr>";
- }
- echo "</table>";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement