I have PHP like this
$associativeArray = array("item1"=>"dogs", "item2"=>"cats",
"item3"=>"rats", "item4"=>"bats");
I want to show this data as a HTML table in this same page.
I have PHP like this
$associativeArray = array("item1"=>"dogs", "item2"=>"cats",
"item3"=>"rats", "item4"=>"bats");
I want to show this data as a HTML table in this same page.
You need to loop through your array:
<table>
<tbody>
<?php
foreach($associativeArray as $key => $index) {
?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $index; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
Try this Code:
</thead>
<tbody>
<?php
foreach ($associativeArray as $key => $value)
{
echo'<tr>';
echo'<td>'. $key .'</td>';
echo'<td>'. $value .'</td>';
echo'<tr>';
}
?>
</tbody>
In order to loop an associate array it you need something like:
foreach ($array_expression as $key => $value) {
echo $key;
echo $value;
}
You should construct your table
, before, during and after the loop, i.e.:
<table>
<tbody>
<tr>
foreach ($array_expression as $key => $value) {
echo "<td>$value</td>";
}
</tr>
</tbody>
</table>
Your final code may look like this:
<?php
echo "<table><tbody><tr>";
foreach ($array_expression as $key => $value) {
echo "<td>$value</td>";
}
echo "</tr></tbody></table>";
It is better to use PHP environment to tabulate your array to avoid confusing:
<?php
$associativeArray = array("item1"=>"dogs", "item2"=>"cats", "item3"=>"rats", "item4"=>"bats");
foreach($associativeArray as $index => $value){
$rows .= "
<tr>
<td>$index</td>
<td>$value</td>
</tr>
";
}
print "
<table>
<tr>
<th>#</th>
<th>value</th>
</tr>
$rows
</table>
";
?>