html - Multi-dimensional arrays php to table rowspan -
i'm stuck @ these.
this array: $tabel3 = array ( array ( "progdas" => "java", "sks" => "3", "prior" => "2" ), array ( "progdas" => "c#", "sks" => "6", "prior" => "1" ), array ( "mat" => "dasar", "sks" => "3", "prior" => "1" ), array ( "mat" => "lanjut", "sks" => "3", "prior" => "3" ), );
and code showing @ tables :
<?php echo "tabel 3<br />"; echo "<table width='auto' border='1'>"; //header echo " <table width='auto' border='1' > <tr> <th colspan='2' scope='col'>mata kuliah</th> <th width='auto' scope='col'>jumlah sks</th> <th width='auto' scope='col'>prioritas</th> </tr> "; foreach ($tabel3 $rows => $row) { //isi echo "<tr>"; if ($cell == "progdas") { echo "<th width='auto' rowspan='2' scope='row'>pemrograman dasar</th>"; foreach ($row $col => $cell) { echo "<td>" . $cell. "</td>"; } } else { echo "<th width='auto' rowspan='2' scope='row'>matematika</th>"; foreach ($row $col => $cell) { echo "<td>" . $cell. "</td>"; } } echo "</tr>"; } echo "</table>"; ?>
i want tables looked :
----------------------------------------------------- mata kuliah | jumlah sks | prioritas | ----------------------------------------------------- pemrograman dasar|_java___|____________|____________| _________________|_c#_____|____________|____________| matematika |_dasar__|____________|____________| _________________|_lanjut_|____________|____________|
does know how rowspan data "pemrograman dasar " , "matematika"? bad result @ code.
first, need rebuild array
foreach ($tabel3 $row) { $row_keys = array_keys($row); $rebuilded[$row_keys[0]][] = $row; }
after can next array:
array ( [progdas] => array ( [0] => array ( [progdas] => java [sks] => 3 [prior] => 2 ) [1] => array ( [progdas] => c# [sks] => 6 [prior] => 1 ) ) [mat] => array ( [0] => array ( [mat] => dasar [sks] => 3 [prior] => 1 ) [1] => array ( [mat] => lanjut [sks] => 3 [prior] => 3 ) )
)
after reorganization can construct html code
<?php echo "tabel 3<br />"; echo "<table width='auto' border='1'>"; echo " <table width='auto' border='1' > <tr> <th colspan='2' scope='col'>mata kuliah</th> <th width='auto' scope='col'>jumlah sks</th> <th width='auto' scope='col'>prioritas</th> </tr> "; foreach ($rebuilded $key => $group) { echo "<tr><th width='auto' rowspan='".count($group)."' scope='row'>".($key == "progdas" ? "pemrograman dasar" : "matematika")."</th>"; foreach ($group $row) { echo "<td>" . ($key == "progdas" ? $row['progdas'] : $row['mat']). "</td><td>" . $row['sks']. "</td><td>" . $row['prior']. "</td></tr>"; }
}
echo "</table>";
?>
Comments
Post a Comment