Menghasilkan Tabel HTML dengan Satu Perulangan di PHP
Mari kita bentuk tabel dengan satu perulangan,
dengan menuliskan elemen-elemen sub-array ke dalam tag
td secara manual:
<?php
echo '<table>';
foreach ($arr as $user) {
echo '<tr>';
echo "<td>{$user['name']}</td>";
echo "<td>{$user['age']}</td>";
echo "<td>{$user['salary']}</td>";
echo '</tr>';
}
echo '</table>';
?>
Cara seperti ini akan memberi kita kontrol yang lebih penuh, baik atas urutan sel maupun atas kemungkinan untuk menambahkan beberapa data tambahan ke setiap sel, misalnya seperti ini:
<?php
echo '<table>';
foreach ($arr as $user) {
echo '<tr>';
echo "<td>{$user['name']}</td>";
echo "<td>{$user['age']} years</td>";
echo "<td>{$user['salary']} dollars</td>";
echo '</tr>';
}
echo '</table>';
?>