Advertisement
cdsatrian

table rowspan

Sep 28th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.54 KB | None | 0 0
  1. <?php
  2. /*
  3. -- use test;
  4. DROP TABLE IF EXISTS `tb_kelas`;
  5. CREATE TABLE IF NOT EXISTS `tb_kelas` (
  6.   `id_kelas` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
  7.   `kelas` varchar(30) NOT NULL,
  8.   PRIMARY KEY (`id_kelas`)
  9. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
  10.  
  11. INSERT INTO `tb_kelas`(`id_kelas`,`kelas`) VALUES(1,"Kelas 1"),(2,"Kelas 2"),(3,"Kelas 3");
  12.  
  13. DROP TABLE IF EXISTS `tb_siswa`;
  14. CREATE TABLE IF NOT EXISTS `tb_siswa` (
  15.   `id_siswa` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  16.   `id_kelas` tinyint(3) UNSIGNED NOT NULL,
  17.   `nama` varchar(30) NOT NULL,
  18.   PRIMARY KEY (`id_siswa`)
  19. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
  20.  
  21. INSERT INTO `tb_siswa`(`id_siswa`,`id_kelas`,`nama`) VALUES(1,1,"Shinta"),(2,2,"Budi"),(3,3,"Angel");
  22.  
  23. DROP TABLE IF EXISTS `tb_jadwal`;
  24. CREATE TABLE IF NOT EXISTS `tb_jadwal` (
  25.   `id_jadwal` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
  26.   `id_kelas` tinyint(3) UNSIGNED NOT NULL,
  27.   `tgl` date NOT NULL,
  28.   `pelajaran` varchar(30) NOT NULL,
  29.   PRIMARY KEY (`id_jadwal`)
  30. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
  31.  
  32. 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");
  33.  
  34.  
  35. */
  36.  
  37.  
  38. $db = new mysqli("localhost", "root", "", "test");
  39. $sql="SELECT JADWAL.tgl, SISWA.nama, KELAS.kelas, JADWAL.pelajaran,
  40.        ( SELECT COUNT(id_jadwal)
  41.          FROM tb_jadwal
  42.          WHERE id_kelas=KELAS.id_kelas) AS jml
  43.      FROM tb_siswa SISWA
  44.        LEFT JOIN tb_kelas KELAS ON KELAS.id_kelas=SISWA.id_kelas
  45.        LEFT JOIN tb_jadwal JADWAL ON JADWAL.id_kelas=KELAS.id_kelas
  46.      ORDER BY JADWAL.tgl,KELAS.id_kelas,SISWA.id_siswa";
  47. $result=$db->query($sql);
  48.  
  49. $no = 1;
  50. $jum = 1;
  51. echo '<table cellpadding="5 " border="1">';
  52. echo '<tr><th>No</th><th>Tgl</th><th>Kelas</th><th>Siswa</th><th>Pelajaran</th></tr>';
  53. while($row=$result->fetch_object()) {          
  54.     echo '<tr>';
  55.     if($jum <= 1) {
  56.         echo '<td align="center" rowspan="'.$row->jml.'">'.$no.'</td>';
  57.         echo '<td rowspan="'.$row->jml.'">'.$row->tgl.'</td>';  
  58.         echo '<td rowspan="'.$row->jml.'">'.$row->nama.'</td>';  
  59.         echo '<td rowspan="'.$row->jml.'">'.$row->kelas.'</td>';  
  60.         $jum = $row->jml;      
  61.         $no++;                    
  62.     } else {
  63.         $jum = $jum - 1;
  64.     }
  65.     echo '<td>'.$row->pelajaran.'</td>';           
  66.     echo '</tr>';                      
  67. }
  68. echo '</table>';
  69. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement