Read 177 times | Created 2013-07-04 02:37:25 | Updated 2013-07-05 03:50:53 | | |

 

<?php
/*
DROP TABLE IF EXISTS `siswa`;
CREATE TABLE IF NOT EXISTS `siswa` (
  `kelas` char(1) NOT NULL,
  `nama` varchar(10) NOT NULL,
  `gender` char(1) NOT NULL,
  `absen` int(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `siswa`
--

INSERT INTO `siswa` (`kelas`, `nama`, `gender`,`absen`) VALUES
('A', 'RUDI', 'L',0),
('A', 'WAHYU', 'L',2),
('A', 'ADI', 'L',1),
('A', 'ZAENAL', 'L',1),
('B', 'KAMIL', 'L',4),
('B', 'LOREN', 'P',1),
('B', 'SARI', 'P',0),
('C', 'RUDI', 'L',1),
('C', 'TUTI', 'P',1);
*/
?>
<!DOCTYPE html>
<html>
<head>
<title>Daftar Siswa</title>
</head>
<body>
<table border='1'>
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='test';
$db=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$sql= 'SELECT * FROM siswa ORDER BY kelas ASC';
$result=$db->query($sql);
$kelas='';
$jml=0;
$total=0;
$absen=array();
while($row=$result->fetch_object()){
  if ($row->kelas!=$kelas)
  {
    echo ($kelas!=''?"<tr><th>Total</th><th>{$jml}</th><th>{$absen[$kelas]}</th></tr>n":"");
    echo "<tr><th colspan='3'>Kelas '{$row->kelas}'</th></tr>n"
        ."<tr><th>NAMA</th><th>GENDER</th><th>ABSEN</th><tr>n";
    $kelas=$row->kelas;
    $total+=$jml;
    $jml=0;
    $absen[$kelas]=0;
  }
  $jml++;
  echo "<tr><td>{$row->nama}</td><td>{$row->gender}</td><td>{$row->absen}</td></tr>n";
  $absen[$kelas]+=$row->absen;
}
$total+=$jml;
echo "<tr><th>Total</th><th>{$jml}</th><th>{$absen[$kelas]}</th></tr>n"
    ."<tr><th>GrandTotal</th><th>{$total}</th><th>".array_sum($absen)."</th></tr>n";
$db->close();
?>
</table>
</body>
</html>