Advertisement
cdsatrian

rowspan simple example

May 29th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. <?php
  2. /*
  3. -- use test;
  4.  
  5. DROP TABLE IF EXISTS `barang`;
  6. CREATE TABLE IF NOT EXISTS `barang` (
  7.   `id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
  8.   `nama` varchar(30) NOT NULL,
  9.   `barang` varchar(30) NOT NULL,
  10.   PRIMARY KEY (`id`)
  11. ) ENGINE=MyISAM;
  12.  
  13. INSERT INTO `barang`(`id`,`nama`,`barang`)
  14. VALUES(1,'andi','mobil'),(2,'andi','motor'),(3,'kiki','sepeda'),(4,'kiki','bb'),(5,'kiki','ipad');
  15.  
  16. */
  17. //=== DATABASE CONNECTION===
  18. $dbhost='localhost';
  19. $dbuser='root';
  20. $dbpass='';
  21. $dbname='test';
  22. $db = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  23. ?>
  24. <!DOCTYPE html>
  25. <html>
  26. <head>
  27. <title>Rowspan Example</title>
  28. </head>
  29. <body>
  30. <table border='1'>
  31. <?php
  32. $sql= "SELECT nama,GROUP_CONCAT(barang) AS brg "
  33.      ."FROM barang "
  34.      ."GROUP BY nama";
  35. $result=$db->query($sql);
  36. while($row=$result->fetch_object())
  37. {
  38.   $barang=explode(',',$row->brg);
  39.   $jml=count($barang);
  40.   for($i=0;$i<$jml;$i++){
  41.     echo ($i==0?"<tr><td rowspan='{$jml}'>{$row->nama}</td>":"<tr>")
  42.         ."<td>{$barang[$i]}</td></tr>\n";
  43.   }
  44. }
  45. ?>
  46. </table>
  47. </body>
  48. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement