Read 364 times | Created 2013-01-14 07:41:06 | Updated 2013-01-14 07:42:18 | | |

 

<?php
/*
-- use test;
DROP TABLE IF EXISTS `tb_kelas`;
CREATE TABLE IF NOT EXISTS `tb_kelas` (
  `id_kelas` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
  `kelas` varchar(30) NOT NULL,
  PRIMARY KEY (`id_kelas`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

INSERT INTO `tb_kelas`(`id_kelas`,`kelas`) 
VALUES
(1,"Kelas 1"),
(2,"Kelas 2"),
(3,"Kelas 3");

DROP TABLE IF EXISTS `tb_siswa`;
CREATE TABLE IF NOT EXISTS `tb_siswa` (
  `id_siswa` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_kelas` tinyint(3) UNSIGNED NOT NULL,
  `nama` varchar(30) NOT NULL,
  PRIMARY KEY (`id_siswa`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

INSERT INTO `tb_siswa`(`id_siswa`,`id_kelas`,`nama`) 
VALUES
(1,1,"Shinta"),
(2,2,"Budi"),
(3,3,"Angel");

DROP TABLE IF EXISTS `tb_jadwal`;
CREATE TABLE IF NOT EXISTS `tb_jadwal` (
  `id_jadwal` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_kelas` tinyint(3) UNSIGNED NOT NULL,
  `tgl` date NOT NULL,
  `pelajaran` varchar(30) NOT NULL,
  PRIMARY KEY (`id_jadwal`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

INSERT INTO `tb_jadwal`(`id_jadwal`,`id_kelas`,`tgl`,`pelajaran`) 
VALUES
(1,1,"2012-09-02","Matematika"),
(2,1,"2012-09-02","Bahasa Indonesia"),
(3,1,"2012-09-02","Biologi"),
(4,2,"2012-09-02","IPS"),
(5,2,"2012-09-02","IPA"),
(6,2,"2012-09-02","Matematika"),
(7,3,"2012-09-02","Fisika"),
(8,3,"2012-09-02","Kimia");


*/
/**** DATABASE CONFIGURATION ****/
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "test";
/**** INITIATE DB CONNECTION ****/
$db = new mysqli($db_host, $db_user, $db_pass, $db_name);
/******* QUERY DATA TO DB *******/
$sql="SELECT JADWAL.tgl, SISWA.nama, KELAS.kelas, JADWAL.pelajaran,
        ( SELECT COUNT(id_jadwal) 
          FROM tb_jadwal 
          WHERE id_kelas=KELAS.id_kelas) AS jml
      FROM tb_siswa SISWA
        LEFT JOIN tb_kelas KELAS ON KELAS.id_kelas=SISWA.id_kelas
        LEFT JOIN tb_jadwal JADWAL ON JADWAL.id_kelas=KELAS.id_kelas
      ORDER BY JADWAL.tgl,KELAS.id_kelas,SISWA.id_siswa";
$result=$db->query($sql);
/***** VARIABLE INITIATION *****/
$no = 1;
$jum = 1;
/****** OUTPUT TO BROWSER ******/
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Contoh Penggunaan RowSpan</title>
    <style type="text/css">
      body,table {padding:0px;margin:0px;font-family:Verdana,Arial,sans-serif;font-size:10px;color:#009;}
      th,tr,td {padding:2px;}
    </style>
  </head>
  <body>
    <table cellpadding="5 " border="1">
      <tr><th>No</th><th>Tgl</th><th>Kelas</th><th>Siswa</th><th>Pelajaran</th></tr>
<?php      
while($row=$result->fetch_object()) {
    echo '<tr>';
    if($jum <= 1) {
        echo '<td align="center" rowspan="'.$row->jml.'">'.$no.'</td>';
        echo '<td rowspan="'.$row->jml.'">'.$row->tgl.'</td>';  
        echo '<td rowspan="'.$row->jml.'">'.$row->nama.'</td>';  
        echo '<td rowspan="'.$row->jml.'">'.$row->kelas.'</td>';  
        $jum = $row->jml;
        $no++;
    } else {
        $jum = $jum - 1;
    }
    echo '<td>'.$row->pelajaran.'</td>';
    echo '</tr>';
}
?>
    </table>
  </body>
</html>