Foreach Loop Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zeusofolus
Forum Newbie
Posts: 1
Joined: Sat Jul 18, 2009 8:40 am

Foreach Loop Error

Post by zeusofolus »

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.
http://cubicalstudio.com/wholesale/foreachlooperror.php

And here is my page code: The Portion I think is causing the error is in red;

Code: Select all

 
<!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 ) { // <=========== red was here 
    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;
}// <======== and finished here
 
 
?>
 
<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
Last edited by Weirdan on Sat Jul 18, 2009 12:26 pm, edited 1 time in total.
Reason: added [code=php] tags
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Foreach Loop Error

Post by Benjamin »

:arrow: Moved to PHP - Code
Post Reply