donnykurnia

Untitled

Nov 18th, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. global $link;
  3. $pilih = "SELECT nama FROM siswa ORDER BY rand()";
  4. $result = mysqli_query($link, $pilih);
  5. //simpan dulu ke array biar mudah loopingnya
  6. $array_nama = [];
  7. while ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
  8.     $array_nama[] = $row['nama'];
  9. }
  10. ?>
  11. <table class="table table-striped text-center">
  12.     <tr class="success">
  13.         <td><strong>No. </strong></td>
  14.         <td><strong>Senin</strong></td>
  15.         <td><strong>Selasa</strong></td>
  16.         <td><strong>Rabu</strong></td>
  17.         <td><strong>Kamis</strong></td>
  18.         <td><strong>Jum'at</strong></td>
  19.         <td><strong>Sabtu</strong></td>
  20.     </tr>
  21.     <?php if ( count($array_nama) > 0 ) { ?>
  22.         <?php
  23.         //kita hitung jumlah baris yang ada yaitu jumlah elemen array bagi 7 bulatkan ke atas
  24.         $jumlah_baris = ceil(count($array_nama) / 7.0);
  25.         ?>
  26.         <?php for( $baris = 1; $baris <= $jumlah_baris; $baris++ ) { ?>
  27.             <tr>
  28.                 <td><?php echo $baris; ?></td>
  29.                 <?php for( $kolom = 1; $kolom <= 7; $kolom++ ) { ?>
  30.                     <?php
  31.                     //rumus index array adalah (baris - 1) * 7 + (kolom - 1) karena index array mulai dari 0
  32.                     //di baris pertama, (baris - 1) = 0, jadi kita akan ambil elemen array dari 0 sampai 6
  33.                     //di baris kedua, (baris - 1) = 1 lalu * 7 = 7, jadi kita ambil elemen array dari 7 sampai 13
  34.                     //begitu selanjutnya selama masih ada elemen array
  35.                     $index = ( $baris - 1 ) * 7 + ( $kolom - 1 );
  36.                     ?>
  37.                     <?php if ( isset($array_nama[$index]) ) { ?>
  38.                         <td><?php echo $row['nama']; ?></td>
  39.                     <?php } else { ?>
  40.                         <td>&nbsp;</td>
  41.                     <?php } ?>
  42.                 <?php } ?>
  43.             </tr>
  44.         <?php } ?>
  45.     <?php } ?>
  46. </table>
Advertisement
Add Comment
Please, Sign In to add comment