title: PHP Shopping Cart Total Price Not Calculating Correctly
body: <p>Interesting issue here...don't know where the problem is because I am using the code from another site that I have.  The first site works perfectly and no problems...but when I converted it over to my new site, the total price amount won't calculate properly.</p>
  
  <p>QUESTION: Am I missing something when I switched over my code?  If I put the code below into the while loop it works ok with only ONE item, but not multiple and it doesn't look right if I change the location to this. </p>
  
  <pre><code>&lt;div class="column text-lg"&gt;Subtotal: &lt;span class="text-medium"&gt;$&lt;?php echo $totalamount; ?&gt;&lt;/span&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Here is some images and the code that I have:</p>
  
  <p><a href="https://i.stack.imgur.com/iSxce.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iSxce.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/35u7E.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/35u7E.jpg" alt="enter image description here"></a></p>
  
  <p>OLD WORKING CODE:</p>
  
  <pre><code>&lt;?php
  if ( ! isset($totalamount)) {
  $totalamount=0;
  }
  $totalquantity=0;
  if (!session_id()) {
  session_start();
  }
  include ('core/connectdb.php');
  $sessid = session_id();
  $query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
  $results = mysqli_query($connect, $query) or die (mysql_query());
  if(mysqli_num_rows($results)==0)
  {
  echo '&lt;div id="content" class="float_r"&gt;&lt;div align="center"&gt;&lt;h3&gt;Your cart is empty.&lt;/h3&gt; You can find our items on our &lt;a href="products.php"&gt;product page&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;div class="cleaner"&gt;&lt;/div&gt;';
  }
  else
  {
  ?&gt;
  &lt;div id="content" class="float_r"&gt;
  &lt;div align="center"&gt;&lt;h1&gt;Shopping Cart&lt;/h1&gt;&lt;/div&gt;
  &lt;table border="1" align="center" cellpadding="5"&gt;
  &lt;tr&gt;&lt;td&gt; Item Code&lt;/td&gt;&lt;td&gt;Quantity&lt;/td&gt;&lt;td&gt;Item Name&lt;/td&gt;&lt;td&gt;Price&lt;/
  td&gt;&lt;td&gt;Total Price&lt;/td&gt;
  &lt;?php
  while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
  extract($row);
  echo "&lt;tr&gt;&lt;td&gt;";
  echo $cart_itemcode;
  echo "&lt;/td&gt;";
  echo "&lt;td&gt;&lt;form method=\"POST\" action=\"cart.php?action=change&amp;icode=
  $cart_itemcode\"&gt;&lt;input type=\"text\" name=\"modified_quantity\" size=\"2\"
  value=\"$cart_quantity\"&gt;";
  echo "&lt;/td&gt;&lt;td&gt;";
  echo $cart_item_name;
  echo "&lt;/td&gt;&lt;td&gt;";
  echo '$' . $cart_price . '';
  echo "&lt;/td&gt;&lt;td&gt;";
  $totalquantity = $totalquantity + $cart_quantity;
  $totalprice = number_format($cart_price * $cart_quantity, 2);
  $totalamount=$totalamount + ($cart_price * $cart_quantity);
  echo '$' . $totalprice . '';
  echo "&lt;/td&gt;&lt;td&gt;";
  echo "&lt;input type=\"submit\" name=\"Submit\" value=\"Change quantity\"&gt;
  &lt;/form&gt;&lt;/td&gt;";
  echo "&lt;td&gt;";
  echo "&lt;form method=\"POST\" action=\"cart.php?action=delete&amp;icode=$cart_itemcode\"&gt;";
  echo "&lt;input type=\"submit\" name=\"Submit\" value=\"Delete Item\"&gt;&lt;/form&gt;
  &lt;/td&gt;&lt;/tr&gt;";
  }
  echo "&lt;tr&gt;&lt;td &gt;Total&lt;/td&gt;&lt;td&gt;$totalquantity&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;";
  $totalamount = number_format($totalamount, 2);
  echo '$' . $totalamount . '';
  echo "&lt;/td&gt;&lt;/tr&gt;";
  echo "&lt;/table&gt;&lt;br&gt;";
  echo "&lt;div style=\"width:400px; margin:auto;\"&gt;You currently have " .
  $totalquantity . " product(s) selected in your cart&lt;/div&gt; ";
  ?&gt;
  &lt;table border="0" style="margin:auto;"&gt;
  &lt;tr&gt;
  &lt;td&gt;&lt;button style="font-family:verdana; font-size:150%;" onclick="goBack()"&gt;Go Back&lt;/button&gt;&lt;/td&gt;
  &lt;td style="padding: 10px;"&gt;
  &lt;form method="POST" action="cart.php?action=empty"&gt;
  &lt;input type="submit" name="Submit" value="Empty Cart"
  style="font-family:verdana; font-size:150%;" &gt;
  &lt;/form&gt;
  &lt;/td&gt;&lt;td&gt;
  &lt;?php include('cart_upload.php'); ?&gt;
  &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  &lt;/div&gt;
  &lt;div class="cleaner"&gt;&lt;/div&gt;
  &lt;?php
  }
  ?&gt;
  </code></pre>
  
  <p>NEW NON-WORKING CODE:</p>
  
  <pre><code>&lt;?php
  if ( ! isset($totalamount)) {
  $totalamount=0;
  }
  $totalquantity=0;
  if (!session_id()) {
  session_start();
  }
  include ('core/connectdb.php');
  $sessid = session_id();
  $query = "SELECT * FROM cart WHERE cart_sess = '$sessid'";
  $results = mysqli_query($connect, $query) or die (mysql_query());
  if(mysqli_num_rows($results)==0)
  {
  echo '&lt;div"&gt;&lt;div align="center"&gt;&lt;h3&gt;Your cart is empty.&lt;/h3&gt; You can find our items on our &lt;a href="products.php"&gt;product page&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;';
  }
  else
  {
  ?&gt;
      &lt;!-- Page Title--&gt;
        &lt;div class="page-title"&gt;
          &lt;div class="container"&gt;
            &lt;div class="column"&gt;
              &lt;h1&gt;Cart&lt;/h1&gt;
            &lt;/div&gt;
            &lt;div class="column"&gt;
              &lt;ul class="breadcrumbs"&gt;
                &lt;li&gt;&lt;a href="index.php"&gt;Home&lt;/a&gt;
                &lt;/li&gt;
                &lt;li class="separator"&gt;&amp;nbsp;&lt;/li&gt;
                &lt;li&gt;Cart&lt;/li&gt;
              &lt;/ul&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;!-- Page Content--&gt;
        &lt;div class="container padding-bottom-3x mb-1"&gt;
          &lt;!-- Shopping Cart--&gt;
          &lt;div class="table-responsive shopping-cart"&gt;
            &lt;table class="table"&gt;
              &lt;thead&gt;
                &lt;tr&gt;
                  &lt;th&gt;Product Name&lt;/th&gt;
                  &lt;th class="text-center"&gt;Quantity&lt;/th&gt;
                  &lt;th class="text-center"&gt;Subtotal&lt;/th&gt;
                  &lt;th class="text-center"&gt;&lt;a class="btn btn-sm btn-outline-danger" href="#"&gt;Clear Cart&lt;/a&gt;&lt;/th&gt;
                &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tdbody&gt;
              &lt;?php
              while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
              extract($row);
              $cart_price = number_format($cart_price);
                echo '&lt;tr&gt;';
                  echo '&lt;td&gt;';
                    echo '&lt;div class="product-item"&gt;&lt;a class="product-thumb" href="shop-single.php?item=' . $cart_itemcode . ' "&gt;&lt;img src="' . $cart_imagename . '" alt="' . $cart_item_name . '"&gt;&lt;/a&gt;';
                      echo '&lt;div class="product-info"&gt;';
                        echo '&lt;h4 class="product-title"&gt;&lt;a href="shop-single.php?item=' . $cart_itemcode . ' "&gt;' . $cart_item_name . '&lt;/a&gt;&lt;/h4&gt;';
                      echo '&lt;/div&gt;';
                    echo '&lt;/div&gt;';
                  echo '&lt;/td&gt;';
                  echo '&lt;td class="text-center"&gt;';
                    echo '&lt;div class="count-input"&gt;';
                    echo '$' . $cart_price . ' Each';
                        echo "&lt;form method=\"POST\" action=\"cart.php?action=change&amp;icode=
                              $cart_itemcode\"&gt;&lt;input type=\"text\" name=\"modified_quantity\" size=\"2\"
                              value=\"$cart_quantity\"&gt;&lt;br\&gt;&lt;input type=\"submit\" name=\"Submit\" value=\"Update\"&gt;
                              &lt;/form&gt;";
                    echo '&lt;/div&gt;';
                  echo '&lt;/td&gt;';
                  $totalquantity = $totalquantity + $cart_quantity;
                  $totalprice = number_format($cart_price * $cart_quantity);
                  $totalamount= number_format($totalamount + ($cart_price * $cart_quantity));
                  echo '&lt;td class="text-center text-lg text-medium"&gt;$' . $totalprice . '&lt;/td&gt;';
                  echo '&lt;td class="text-center"&gt;&lt;a class="remove-from-cart" href="cart.php?action=delete&amp;icode=' . $cart_itemcode . '" data-toggle="tooltip" title="Remove item"&gt;&lt;i class="icon-cross"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;';
                echo '&lt;/tr&gt;';
              }
  }
  ?&gt;
  &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;div class="shopping-cart-footer"&gt;
  &lt;div class="column text-lg"&gt;Subtotal: &lt;span class="text-medium"&gt;$&lt;?php echo $totalamount; ?&gt;&lt;/span&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="shopping-cart-footer"&gt;
  &lt;div class="column"&gt;&lt;a class="btn btn-outline-secondary" onclick="goBack()"&gt;&lt;i class="icon-arrow-left"&gt;&lt;/i&gt;&amp;nbsp;Back to Shopping&lt;/a&gt;&lt;/div&gt;
  &lt;div class="column"&gt;&lt;a class="btn btn-primary" href="#" data-toast data-toast-type="success" data-toast-position="topRight" data-toast-icon="icon-circle-check" data-toast-title="Your cart" data-toast-message="is updated successfully!"&gt;Update Cart&lt;/a&gt;&lt;a class="btn btn-success" href="checkout-address.php"&gt;Checkout&lt;/a&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>