I have recently taken part in the Sitepoint PHP course and I am having a problem with a foreach loop.
Warning: Invalid argument supplied for foreach() in C:\wamp\www\…on line 53
The code I have works fine once content has been entered on the form but prior to that I get this warning.
So I think its a case of the table has not had data entered at that point therefore the select
query cant find anything so no array content
question how do I stop this warning appearing before the data is entered.
thanks Kevin
INDEX PHP
include $_SERVER[‘DOCUMENT_ROOT’] . ‘/includes/db.inc.php’;
$sql = “SELECT * FROM items WHERE order_id=‘$id’”;
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = ‘Error fetching item from database!’;
include ‘error.html.php’;
exit();
}
while ($row = mysqli_fetch_array($result))
{
$items = array(‘item_code’ => $row[‘item_code’],
‘item_description’ => $row[‘item_description’],
‘item_price’ => $row[‘item_price’],
‘card_message’ => $row[‘card_message’]);
}
DISPLAY PAGE
<?php foreach ($items as $item): ?>
<li>
<form action="" method="post">
<div>
<p>Item Code...<?php htmlout($item['item_code']); ?></p>
<p>Description...<?php htmlout($item['item_description']); ?></P>
<p>Price...<?php htmlout($item['item_price']); ?></p>
<p>Card Message...<?php htmlout($item['card_message']); ?></p>
<input type="hidden" name="id" value="<?php
echo $item['id']; ?>"/>
</div>
</form>
</li>
<?php endforeach; ?>