Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- global $link;
- $pilih = "SELECT nama FROM siswa ORDER BY rand()";
- $result = mysqli_query($link, $pilih);
- //simpan dulu ke array biar mudah loopingnya
- $array_nama = [];
- while ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
- $array_nama[] = $row['nama'];
- }
- ?>
- <table class="table table-striped text-center">
- <tr class="success">
- <td><strong>No. </strong></td>
- <td><strong>Senin</strong></td>
- <td><strong>Selasa</strong></td>
- <td><strong>Rabu</strong></td>
- <td><strong>Kamis</strong></td>
- <td><strong>Jum'at</strong></td>
- <td><strong>Sabtu</strong></td>
- </tr>
- <?php if ( count($array_nama) > 0 ) { ?>
- <?php
- //kita hitung jumlah baris yang ada yaitu jumlah elemen array bagi 7 bulatkan ke atas
- $jumlah_baris = ceil(count($array_nama) / 7.0);
- ?>
- <?php for( $baris = 1; $baris <= $jumlah_baris; $baris++ ) { ?>
- <tr>
- <td><?php echo $baris; ?></td>
- <?php for( $kolom = 1; $kolom <= 7; $kolom++ ) { ?>
- <?php
- //rumus index array adalah (baris - 1) * 7 + (kolom - 1) karena index array mulai dari 0
- //di baris pertama, (baris - 1) = 0, jadi kita akan ambil elemen array dari 0 sampai 6
- //di baris kedua, (baris - 1) = 1 lalu * 7 = 7, jadi kita ambil elemen array dari 7 sampai 13
- //begitu selanjutnya selama masih ada elemen array
- $index = ( $baris - 1 ) * 7 + ( $kolom - 1 );
- ?>
- <?php if ( isset($array_nama[$index]) ) { ?>
- <td><?php echo $row['nama']; ?></td>
- <?php } else { ?>
- <td> </td>
- <?php } ?>
- <?php } ?>
- </tr>
- <?php } ?>
- <?php } ?>
- </table>
Advertisement
Add Comment
Please, Sign In to add comment