I’m having a problem with php’s for loop.
The loop iterates too fast and is done before the html code inside is echo-ed.
<?php for($i = 0; $i < sizeof($shoppingCart); $i++) {
$sql = "SELECT * FROM products_test WHERE id = " . substr($shoppingCart[$i], 5) . "";
$q = $DBH->query($sql) or die("failed!");
$q->setFetchMode(PDO::FETCH_ASSOC);
$r = $q->fetchAll();
echo '<tr>';
echo '<td><img src="' . $r[$i]['image']. '" /></td>';
echo '<td>' . $r[$i]['title'] . '</td>';
echo '<td>' . $r[$i]['desc'] . '</td>';
echo '<td>' . $r[$i]['price'] . '</td>';
echo '<td><input type="number" id="qty"></td>';
echo '<td>Remove</td>';
echo '</tr>';
} ?>
Setting a temporary variable won’t work because it will just have the same behaviour.
What’s wrong?
Thanks in advance!
It could be that your array is coming up empty. You should add a catch in case.
You would want to make something like this, using the
emptyfunction: