View difference between Paste ID: rpLPHFgY and D3gUhTcW
SHOW: | | - or go back to the newest paste.
1-
<?php $no = 1;
1+
<?php
2-
	global $link;
2+
global $link;
3-
	$pilih = "SELECT nama FROM siswa ORDER BY rand()";
3+
$pilih = "SELECT nama FROM siswa ORDER BY rand()";
4-
	$result = mysqli_query($link,$pilih);
4+
$result = mysqli_query($link, $pilih);
5-
 ?>
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-
	<?php 
16+
17-
		while ($row=mysqli_fetch_array($result)) {
17+
18-
	 ?>
18+
19-
	<tr>
19+
20-
		<td><?php echo $no++; ?></td>
20+
21-
		<td><?php echo $row['nama']; ?></td>
21+
	<?php if ( count($array_nama) > 0 ) { ?>
22-
		<td><?php echo $row['nama']; ?></td>
22+
		<?php
23-
		<td><?php echo $row['nama']; ?></td>
23+
		//kita hitung jumlah baris yang ada yaitu jumlah elemen array bagi 7 bulatkan ke atas
24-
		<td><?php echo $row['nama']; ?></td>
24+
		$jumlah_baris = ceil(count($array_nama) / 7.0);
25-
		<td><?php echo $row['nama']; ?></td>
25+
		?>
26-
		<td><?php echo $row['nama']; ?></td>
26+
		<?php for( $baris = 1; $baris <= $jumlah_baris; $baris++ ) { ?>
27
			<tr>
28-
	<?php 
28+
		        <td><?php echo $baris; ?></td>
29-
		}
29+
				<?php for( $kolom = 1; $kolom <= 7; $kolom++ ) { ?>
30-
	 ?>
30+
					<?php
31-
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>