Hello All,
I have a order form that is using a foreach loop with an array.
The loop displays correctly the first time it is displayed on the page, but when the totals are updated with the submit button the loop duplicates the second to last entry?
Here is a working example with errors noted.
And here is my page code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Foreach Loop Error</title>
<link rel="stylesheet" type="text/css" href="wholesale_overall.css" />
</head>
<body>
<div id="container">
<?php
echo '<h1>Foreach Loop Error</h1>';
require_once('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$items = array();
$query = mysqli_query( $dbc,"SELECT `ItemID`,`ItemName`,`ItemCat`,`ItemPrice`,`ItemAmount`,`ItemUnit` FROM `items` ORDER BY `ItemCat` ASC" );
while( list( $id,$name,$cat,$price,$amount,$unit ) = mysqli_fetch_array( $query ) ) {
$items[$id] = array(
'name' => $name,
'cat' => $cat,
'price' => $price,
'amount' => $amount,
'unit' => $unit,
'id' => $id
);
}
echo '<div id="form_box">';
echo '<div id ="totalbox">';
if ( isset( $_POST['update'] ) ) {
$total = 0;
foreach( $_POST as $key => $value ) {
list( ,$key ) = explode( '-',$key );
$value = (int) $value;
if ( isset( $items[$key] ) ) {
$item =& $items[$key];
$item['quantity'] = $value;
$price = ( $item['price'] * $value );
$tax_rate = 0.035;
$item_tax = ($price * $tax_rate);
$item_tax = number_format($item_tax, 2);
if ($value != 0){
echo "{$item['name']} / {$item['cat']} - \${$item['price']} x {$item['quantity']} = \${$price}<br />";
}
$total += $price;
$tax += $item_tax;
$grand_total = $tax + $total;
}
}
echo "<p class=\"orderinfo\">Sub Total: \${$total}</p>";
echo "<p class=\"orderinfo\">Tax: \${$tax}</p>";
echo "<p class=\"orderinfo\">Grand Total: \${$grand_total}</p>";
echo '<p class="error">NOTE:Once the order has been submitted the last item in the loop is duplicated. However if you enter a value in the last row and update the total again the correct price and product info is calculated for that row?</p>';
}
echo <<<HTML
<form name="orderform" action="{$_SERVER['PHP_SELF']}" method="post">
HTML;
foreach( $items as $id => $item ) {
if ( !isset( $item['quantity'] ) ) {
$item['quantity'] = 0;
}
echo <<<HTML
<div class="product_line">
<div class="prod_name">{$item['name']} / {$item['cat']}</div>
<div class="prod_price">\${$item['price']} ... per ... {$item['amount']} {$item['unit']}
<label for="item-{$id}">Quantity:</label>
<input type="text" name="item-{$id}" size="5" value="{$item['quantity']}" />
</div>
</div>
HTML;
}
?>
<br />
<input type="submit" name="update" value="Update Total" />
</form>
<a href="foreachlooperror.php">RESET FORM</a>
</div>
<?php
mysqli_close( $dbc );
?>
</div> <!-- END OF CONTAINER DIV-->
</body>
</html>
Thanks for Looking!
Eric