Postingan lainnya
Bagaimana membuat table dengan php?
output yang diinginkan seperti ini :
tetapi yang keluar seperti ini :
Coding saya :
<?php
$arr = array(
array(
'kode' => 'A1',
'kuliah' => 'Kalkulus',
'sks' => 4
),
array(
'kode' => 'C7',
'kuliah' => 'Matematika Diskrit',
'sks' => 2
),
array(
'kode' => 'B3',
'kuliah' => 'Etika',
'sks' => 2
),
array(
'kode' => 'E6',
'kuliah' => 'Logika',
'sks' => 2
),
);
?>
<!doctype html>
<html>
<head>
<title>Latihan PHP</title>
<!--Meta-->
<meta charset="UTF-8">
<meta name="description" content="T-5 Intro to PHP">
<meta name="keywords" content="PHP">
<meta name="author" content="MUHAMAD ARIQ RASYID-2020130036">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Import Bootstrap CSS di sini -->
<link rel="stylesheet" href="Asset/Plug-ins/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<table>
<thead>
</thead>
<tbody>
<tr class="table_header">
<td width="66"><strong>No.</strong></td>
<td width="50"><strong>Kode</strong></td>
<td width="100"><strong>Mata Kuliah<strong></td>
<td width="75"><strong>SKS<Strong></td>
</tr>
</tbody>
<?php
$Kode= '';
$Mata_Kuliah = '';
$SKS = '';
foreach($arr as $arr_Code=>$arr_Code_Info){
$rowcount = 1;
foreach($arr_Code_Info as $arrMataKuliah=>$arr_SKS){
echo "<tr>";
echo "<td>";
echo ($rowcount == 1 )? $arr_Code : "" ;
echo "</td>";
echo "<td>";
echo "$arrMataKuliah <br/>";
echo "</td>";
echo "<td>";
foreach($arr_SKS as $arr_TSKS){
echo "$arr_TSKS <br/>";
}
echo "</td>";
echo "</tr>";
$rowcount +=1;
}
}
?>
</table>
</div>
<!-- Import Bootstrap JS di sini -->
<script type="text/javascript" src="Asset/Plug-ins/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
pendapat saya mungkin ada yang salah di baris ke 81(
foreach($arr_SKS as $arr_TSKS) ...
)
tetapi saya tidak tahu harus bagaimana
terima kasih sebelunya ^^
1 Jawaban:
<div>Harusnya cukup 1x looping aja, ngga usah dua kali, kemudian datanya itu kan dalam bentuk array, harusnya kita cukup mengakses berdasarkan key
nya aja. Berikut contoh kode yang sudah diperbaiki, silahkan dicek letak perbedaannya.</div><pre><?php
$arr = array(
array(
'kode' => 'A1',
'kuliah' => 'Kalkulus',
'sks' => 4
),
array(
'kode' => 'C7',
'kuliah' => 'Matematika Diskrit',
'sks' => 2
),
array(
'kode' => 'B3',
'kuliah' => 'Etika',
'sks' => 2
),
array(
'kode' => 'E6',
'kuliah' => 'Logika',
'sks' => 2
),
);
?>
<!doctype html>
<html>
<head> <title>Latihan PHP</title> <!--Meta--> <meta charset="UTF-8"> <meta name="description" content="T-5 Intro to PHP"> <meta name="keywords" content="PHP"> <meta name="author" content="MUHAMAD ARIQ RASYID-2020130036"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
&lt;!-- Import Bootstrap CSS di sini --&gt;
&lt;link rel="stylesheet" href="Asset/Plug-ins/bootstrap/css/bootstrap.min.css"&gt;
</head>
<body> <div class="container mt-5"> <table border="1" cellpadding="3" cellspacing="0"> <thead> <tr class="table_header"> <td width="66"><strong>No.</strong></td> <td width="50"><strong>Kode</strong></td> <td width="100"><strong>Mata Kuliah<strong></td> <td width="75"><strong>SKS<Strong></td> </tr> </thead> <tbody> <?php $rowcount = 1; foreach($arr as $data){ echo "<tr>"; // Pembuka table row
echo "&lt;td&gt;" . $rowcount . "&lt;/td&gt;";
echo "&lt;td&gt;" . $data['kode'] . "&lt;/td&gt;";
echo "&lt;td&gt;" . $data['kuliah'] . "&lt;/td&gt;";
echo "&lt;td&gt;" . $data['sks'] . "&lt;/td&gt;";
echo "&lt;/tr&gt;"; // Penutup table row
$rowcount +=1; // Increment variabe;
}
?&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
<!-- Import Bootstrap JS di sini --> <script type="text/javascript" src="Asset/Plug-ins/bootstrap/js/bootstrap.min.js"></script> </body> </html></pre><div><br><br>Semoga berhasil.</div>