Advertisement
cdsatrian

Tampil Barang Berdasar Kategori

Jul 24th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. <?php
  2. /********************************
  3. / SEARCH BARANG BY KATEGORI
  4. / FILENAME : barang.php
  5. / UPDATED BY : CAHYA DSN
  6. / CREATED DATE : 2013-07-24
  7. /*******************************
  8.  
  9. -- Database: `test`
  10. --
  11. -- Table structure for table `tbl_kategori`
  12. --
  13. DROP TABLE IF EXISTS `tbl_kategori`;
  14. CREATE TABLE IF NOT EXISTS `tbl_kategori` (
  15. `id_kategori` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  16. `kode_kategori` VARCHAR(10)NOT NULL,
  17. `nama_kategori` VARCHAR(50) NOT NULL,
  18. PRIMARY KEY (`id_kategori`)
  19. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  20. -- Dumping data for table `tbl_kategori`
  21. INSERT INTO `tbl_kategori`(`kode_kategori`,`nama_kategori`)
  22. VALUES
  23. ('ELK0010001','Televisi'),
  24. ('ELK0010002','Kulkas'),
  25. ('ELK0010003','Air Conditioner');
  26.  
  27. -- Table structure for table `tbl_barang`
  28. --
  29. DROP TABLE IF EXISTS `tbl_barang`;
  30. CREATE TABLE IF NOT EXISTS `tbl_barang` (
  31. `id_barang` int(11) unsigned NOT NULL AUTO_INCREMENT,
  32. `id_kategori` smallint(6) unsigned NOT NULL,
  33. `kode_barang` VARCHAR(10)NOT NULL,
  34. `nama_barang` VARCHAR(25) NOT NULL,
  35. `deskripsi` VARCHAR(255) NOT NULL,
  36. `stok` int(11) unsigned NOT NULL DEFAULT '0',
  37. `harga` float NOT NULL DEFAULT '0.0',
  38. PRIMARY KEY (`id_barang`)
  39. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  40. -- Dumping data for table `tbl_barang`
  41. INSERT INTO `tbl_barang`(`id_kategori`,`kode_barang`,`nama_barang`,`deskripsi`,`stok`,`harga`)
  42. VALUES
  43. ('1','TV21','Televisi 21 inchi','TV LCD 21 inchi',15,1900000),
  44. ('1','TV24','Televisi 24 inchi','TV LCD 24 inchi',7,2200000),
  45. ('1','TV29','Televisi 29 inchi','TV LCD 29 inchi',9,2600000),
  46. ('1','TV32','Televisi 32 inchi','TV LCD 32 inchi',10,3300000),
  47. ('1','TV40','Televisi 40 inchi','TV LCD 40 inchi',2,5300000),
  48. ('2','KK100P1','Kulkas 1 pintu 100L','Kulkas 1 pintu 100L',5,1200000),
  49. ('2','KK140P1','Kulkas 1 pintu 140L','Kulkas 1 pintu 140L',6,1500000),
  50. ('2','KK220P2','Kulkas 2 pintu 220L','Kulkas 2 pintu 220L',3,3200000),
  51. ('2','KK240P2','Kulkas 2 pintu 240L','Kulkas 2 pintu 240L',4,3700000),
  52. ('2','KK250P2','Kulkas 2 pintu 250L','Kulkas 2 pintu 250L',2,4000000),
  53. ('3','AC12','Air Conditioner 1/2 PK','Air Conditioner 1/2 PK',4,3200000),
  54. ('3','AC34','Air Conditioner 3/4 PK','Air Conditioner 3/4 PK',3,4200000),
  55. ('3','AC01','Air Conditioner 1 PK','Air Conditioner 1 PK',4,5200000),
  56. ('3','AC02','Air Conditioner 2 PK','Air Conditioner 2 PK',2,6100000);
  57. */
  58. // Database Configuration
  59. $dbhost='localhost';
  60. $dbuser='root';
  61. $dbpass='';
  62. $dbname='test';
  63. // Database Connection
  64. $db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  65. ?>
  66. <!DOCTYPE html>
  67. <html>
  68. <head>
  69. <title>Search Barang</title>
  70. </head>
  71. <body>
  72. <form method="POST">
  73. KODE KATEGORI
  74. <select name="id_kategori">
  75. <option>-- pilih salah satu</option>
  76. <?php
  77. $sql="SELECT * FROM tbl_kategori";
  78. $result=$db->query($sql);
  79. while($row=$result->fetch_object())
  80. {
  81. echo "<option value='{$row->id_kategori}'>{$row->kode_kategori} - {$row->nama_kategori}</option>\n";
  82. }
  83. $result->close();
  84. ?>
  85. </select>
  86. <input type="submit" name="submit" value="search" />
  87. </form>
  88. <fieldset>
  89. <legend>Hasil Pencarian</legend>
  90. <div id="search_container">
  91. <?php
  92. if(isset($_POST['submit']))
  93. {
  94. $sql="SELECT k.kode_kategori,k.nama_kategori,b.nama_barang,b.deskripsi,b.stok,b.harga "
  95. ."FROM tbl_barang b "
  96. ."JOIN tbl_kategori k USING(id_kategori) "
  97. ."WHERE k.id_kategori='".(isset($_POST['id_kategori'])?$_POST['id_kategori']:'')."'";
  98. $result=$db->query($sql);
  99. if($result->num_rows>0)
  100. {
  101. echo "<table>\n"
  102. ."<tr>\n"
  103. ."<th>No</th>\n"
  104. ."<th>Kd Kat</th>\n"
  105. ."<th>Kategori</th>\n"
  106. ."<th>Nama</th>\n"
  107. ."<th>Deskripsi</th>\n"
  108. ."<th>Stok</th>\n"
  109. ."<th>Harga</th>\n"
  110. ."</tr>";
  111. $i=0;
  112. while($row=$result->fetch_object())
  113. {
  114. echo "<tr>\n"
  115. ."<td>".++$i."</td>\n"
  116. ."<td>{$row->kode_kategori}</td>\n"
  117. ."<td>{$row->nama_kategori}</td>\n"
  118. ."<td>{$row->nama_barang}</td>\n"
  119. ."<td>{$row->deskripsi}</td>\n"
  120. ."<td align='right'>{$row->stok}</td>\n"
  121. ."<td align='right'>".number_format($row->harga)."</td>\n"
  122. ."</tr>\n";
  123. }
  124. echo "</table>\n";
  125. $result->close();
  126. }
  127. else
  128. {
  129. echo "No data found";
  130. }
  131. }
  132. $db->close();
  133. ?>
  134. </div>
  135. </fieldset>
  136. </body>
  137. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement