Populate php array into HTML table
I have data in a php array (say, $rows2) that gives the below output in key-value pairs. I am trying to format this data into an HTML table:
0 => Month:
1 => 07/2011,
2 => 7-8
3 => / 1432 A.H
4 =>
5 => Location:
6 => Plainfield,
7 => NJ,
8 => USA
9 => Calculation Method:
10 => Value 10
11 =>
12 => Juristic Method:
13 => Standard
I tried to use the below code to populate the table:
echo '<table border=1>';
while (list($key, $value) = each($rows2)) {
echo $value2.$value;
if($i%5 == 0){
echo '<tr><td>'.$value2.'</td></tr>';
}
$value2='';
$i++;
}
echo '</table>';
I get the below output:
--------------------------------------------------------
Month: | 07/2011 | 7-8 | 1432 A.H
--------------------------------------------------------
Location: | Plainfield | NJ | USA
--------------------------------------------------------
Calculation | ValueX | Juristic | ValueY
Method: | | Method: |
--------------------------------------------------------
I am trying to split the last row into 2 rows and then colspan the second to look like the below result.
--------------------------------------------------------
Month: | 07/2011 | 7-8 | 1432 A.H
--------------------------------------------------------
Location: | Plainfield | NJ | USA
--------------------------------------------------------
Calculation | ValueX
Method: |
--------------------------------------------------------
Juristic | ValueY
Method: |
--------------------------------------------------------
Could someone please help? Thanks in advance.
If the following things are true:
Then you're probably better off just inserting parts of the array right into the table, like so:
<table>
<tr>
<td><?= $array[0]?></td>
<td><?= $array[1]?></td>
<td><?= $array[2]?></td>
<td><?= $array[3]?></td>
</tr>
<tr>
<td><?= $array[9]?></td>
<td colspan='3'><?= $array[10]?></td>
</tr>
<tr>
<td><?= $array[12]?></td>
<td colspan='3'><?= $array[13]?></td>
</tr>
</table>
<table>
<?php
foreach( $row in $rows2) {
echo '<tr>';
echo '<td>' . $row[0] . '</td>';
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '<td>' . $row[4] . '</td>';
echo '</tr><tr>';
echo '<td>' . $row[5] . '</td>';
echo '<td>' . $row[6] . '</td>';
echo '<td>' . $row[7] . '</td>';
echo '<td>' . $row[8] . '</td>';
echo '</tr><tr>';
echo '<td>' . $row[9] . '</td>';
echo '<td colspan="3">' . $row[10] . '</td>';
echo '</tr><tr>';
echo '<td>' . $row[12] . '</td>';
echo '<td colspan="3">' . $row[13] . '</td>';
echo '</tr>';
} // end foreach
?>
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With