Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- -- use test;
- DROP TABLE IF EXISTS `barang`;
- CREATE TABLE IF NOT EXISTS `barang` (
- `id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
- `nama` varchar(30) NOT NULL,
- `barang` varchar(30) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM;
- INSERT INTO `barang`(`id`,`nama`,`barang`)
- VALUES(1,'andi','mobil'),(2,'andi','motor'),(3,'kiki','sepeda'),(4,'kiki','bb'),(5,'kiki','ipad');
- */
- //=== DATABASE CONNECTION===
- $dbhost='localhost';
- $dbuser='root';
- $dbpass='';
- $dbname='test';
- $db = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Rowspan Example</title>
- </head>
- <body>
- <table border='1'>
- <?php
- $sql= "SELECT nama,GROUP_CONCAT(barang) AS brg "
- ."FROM barang "
- ."GROUP BY nama";
- $result=$db->query($sql);
- while($row=$result->fetch_object())
- {
- $barang=explode(',',$row->brg);
- $jml=count($barang);
- for($i=0;$i<$jml;$i++){
- echo ($i==0?"<tr><td rowspan='{$jml}'>{$row->nama}</td>":"<tr>")
- ."<td>{$barang[$i]}</td></tr>\n";
- }
- }
- ?>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement