Read 370 times | Created 2013-01-20 03:53:34 | Updated 2013-10-08 09:50:13 | | |

 

<?php
/*********************************
FILENAME  : multirowspan.php
CREATE BY  : cahya dsn
PURPOSE   : show grouping data on rowspan table
CREATE DATE : 2013-01-20
**********************************
#table creation

USE `test`;

DROP TABLE IF EXISTS `t_produk`;
CREATE TABLE IF NOT EXISTS `t_produk` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key untuk table t_produk',
 `kategori` varchar(50) NOT NULL COMMENT 'kategori',
 `produk` varchar(50) NOT NULL COMMENT 'produk',
 `warna` varchar(10) NOT NULL COMMENT 'warna',
 `harga` float unsigned NOT NULL DEFAULT 0.0,
 PRIMARY KEY(`id`)
) ENGINE=MyISAM COMMENT='tabel untuk menyimpan data produk';

INSERT INTO `t_produk`(`kategori`,`produk`,`warna`,`harga`)
VALUES ('sepatu','sepatu 1','biru',115000.0),
('sepatu','sepatu 1','putih',110000.0),
('sepatu','sepatu 2','hitam',120000.0),
('sepatu','sepatu 2','coklat',120000.0),
('kaos','kaos 1','biru',80000.0),
('kaos','kaos 2','coklat',75000.0),
('kaos','kaos 2','merah',60000.0),
('kaos','kaos 3','hijau',60000.0);
*/
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='test';
$db = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$result=$db->query('SELECT * FROM t_produk');
$data=array();
$i=0;
while($row=$result->fetch_assoc()){
	$data[$i]=$row;
	$i++;
}
$total = array();
$katPrev = $prodPrev = $hargaPrev= '';
$i=0;
foreach($data as $cell) {
	if(isset($total[$cell['kategori']]['jml'])) {
		$total[$cell['kategori']]['jml']++;
	} else {
		$total[$cell['kategori']]=array();
		$total[$cell['kategori']]['jml']=1;
	}
	if(isset($total[$cell['kategori']][$cell['produk']]['jml'])) {
		$total[$cell['kategori']][$cell['produk']]['jml']++;
	} else {
		$total[$cell['kategori']][$cell['produk']]=array();
		$total[$cell['kategori']][$cell['produk']]['jml']=1;
	}
	if(isset($total[$cell['kategori']][$cell['produk']][$cell['harga']]['jml'])) {
		$total[$cell['kategori']][$cell['produk']][$cell['harga']]['jml']++;
	} else {
		$total[$cell['kategori']][$cell['produk']][$cell['harga']]['jml']=1;
	}
	$i++;
}
?>
<!DOCTYPE html>
<html>
	<head>
		<title>Multi RowSpan</title>
		<style type="text/css">
			table {font-family:arial,verdana,sans-serif;
                               font-size:12px;padding:0px;margin:0px;
                               border-spacing:0;border-collapse:collapse;}
			tr {padding:0px;margin:0px;}
			th {background-color:#333;color:#fff;
                            border:solid 1px #999;padding:5px;margin:0px;}
			td {border:solid 1px #999;margin:0px;padding:5px;}
		</style>
	</head>
	<body>
		<table>
			<tr>
				<th>KATEGORI</th>
				<th>PRODUK</th>
				<th>WARNA</th>
				<th>HARGA</th>
			</tr>
<?php
$n=count($data);
for($i=0;$i<$n;$i++){
	$cell=$data[$i];	
	echo '<tr>';
	if($katPrev!=$cell['kategori']) {
		echo '<td'
		    .($total[$cell['kategori']]['jml']>1?' rowspan="'
                    .($total[$cell['kategori']]['jml']).'">':'>')
		    .$cell['kategori'].'</td>';
		$katPrev=$cell['kategori'];
	}
	if($prodPrev!=$cell['produk']) {
		echo '<td'
		    .($total[$cell['kategori']][$cell['produk']]['jml']>1?' rowspan="'
                    .($total[$cell['kategori']][$cell['produk']]['jml']).'">':'>')
		    .$cell['produk'].'</td>';
		$prodPrev=$cell['produk'];
	}
	echo '<td>'.$cell['warna'].'</td>';
	if($hargaPrev!=$cell['harga'] || $prodPrev!=$data[$i-1]['produk']) {
		echo '<td'
		    .($total[$cell['kategori']][$cell['produk']][$cell['harga']]['jml']>1?' rowspan="'
                    .($total[$cell['kategori']][$cell['produk']][$cell['harga']]['jml']).'">':'>')
		    .$cell['harga'].'</td>';
		$hargaPrev=$cell['harga'];
	}
	echo "</tr>n";
}
?>
	</table>
	</body>
</html>