I'm trying to display data in html using a PHP foreach loop. I get my data array ($studentHistory
in the code below) by calling PDOStatement::fetchAll
after query results are returned.
However, when I write out html in the foreach loop using echo,
values in the inner loop are skipping one iteration.
echo '<table>
<tr>
<th>Type</th>
<th>STUDENT ID</th>
<th>ROOM ID</th>
<th>DATE</th>
<th>Description</th>
</tr>
';
foreach ($studentHistory as $item=>$row)
{
echo "<tr>";
foreach($row as $value)
{
echo "<td style='width: 150px; border: 1px solid black;>".$value."</td>";
}
echo "</tr>";
}
echo '</table>';
If I replace the inner loop to echo without HTML, it shows all data:
...
foreach($row as $value)
{
echo $value." ";
}
...
I'm using PDO and running MySQL 5.
How can I solve this?
foreach
loop. Just output the value directly from the$row
variable. Ex.$row['name_of_column']
. But we can't verify that without seeing your code. – waterloomatt Jul 8 '19 at 17:06echo "<td style='width: 150px;border: 1px solid black;'>".$value."</td>";
– Always Sunny Jul 8 '19 at 17:07