title: Total amount doesnt show from Shopping Cart
body: <p><strong>Background Story:</strong> Shopping Cart wont display Total and Cart Total! I got the code via <a href="http://www.youtube.com/playlist?list=PL442E340A42191003" rel="nofollow">http://www.youtube.com/playlist?list=PL442E340A42191003</a> and i did some modification.</p>
  
  <p><strong>Here is the image view of the problem:</strong> 
  <a href="http://i.imgur.com/tkMrvZv.jpg" rel="nofollow">http://i.imgur.com/tkMrvZv.jpg</a></p>
  
  <p><strong>What i did to debug:</strong> I tried to debug by echoing variables for testing to make sure the variables stored is of right value</p>
  
  <p>Total (Individual Product) and Cart Total needs to be derived from
   <strong>$pricetotal = $price * $each_item['quantity'];</strong></p>
  
  <p>I debug by testing echo <strong>$price</strong> and echo <strong>$each_item['quantity']</strong> Both showed the right value in it. However to my surprise, echo <strong>$pricetotal</strong> showed result of $0.00! I then moved on to changing <strong>$pricetotal = $price + $each_item['quantity'];</strong> In this case, by echoing <strong>$pricetotal</strong> , i got the value of 2. (2 quantity) and the total/Cart total became $2.00. </p>
  
  <hr>
  
  <p><strong>Conclusion from the experiment:</strong></p>
  
  <ul>
  <li>Individual echoing of <strong>$price</strong> and <strong>$each_item['quantity']</strong>
  showed right. </li>
  <li>echoing <strong>$pricetotal</strong> only gave    <strong>$each_item['quantity']</strong> and
  no value of <strong>$price</strong> existed.</li>
  </ul>
  
  <p>On the other hand, the PayPal checkout button works and PayPal is able to obtain the product amount,quantity and Total.</p>
  
  <p>May i know what is wrong??</p>
  
  <p>Here are the code for reference:</p>
  
  <pre><code>&lt;? ob_start(); ?&gt;
  &lt;?php 
  // This file is www.developphp.com curriculum material
  // Written by Adam Khoury January 01, 2011
  // http://www.youtube.com/view_play_list?p=442E340A42191003
  session_start(); // Start session first thing in script
  // Script Error Reporting
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
  // Connect to the MySQL database  
  include "MyOnlineStore/storescripts/connect_to_mysql.php"; 
  ?&gt;
  &lt;?php 
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //       Section 1 (if user attempts to add something to the cart from the product page)
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (isset($_POST['pid'])) {
      $pid = $_POST['pid'];
      $wasFound = false;
      $i = 0;
      // If the cart session variable is not set or cart array is empty
      if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) &lt; 1) { 
          // RUN IF THE CART IS EMPTY OR NOT SET
          $_SESSION["cart_array"] = array(0 =&gt; array("item_id" =&gt; $pid, "quantity" =&gt; 1));
      } else {
          // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
          foreach ($_SESSION["cart_array"] as $each_item) { 
                $i++;
                while (list($key, $value) = each($each_item)) {
                    if ($key == "item_id" &amp;&amp; $value == $pid) {
                        // That item is in cart already so let's adjust its quantity using array_splice()
                        array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" =&gt; $pid, "quantity" =&gt; $each_item['quantity'] + 1)));
                        $wasFound = true;
                    } // close if condition
                } // close while loop
             } // close foreach loop
             if ($wasFound == false) {
                 array_push($_SESSION["cart_array"], array("item_id" =&gt; $pid, "quantity" =&gt; 1));
             }
      }
      header("location: cart.php"); 
      exit();
  }
  ?&gt;
  &lt;?php 
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //       Section 2 (if user chooses to empty their shopping cart)
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (isset($_GET['cmd']) &amp;&amp; $_GET['cmd'] == "emptycart") {
      unset($_SESSION["cart_array"]);
  }
  ?&gt;
  &lt;?php 
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //       Section 3 (if user chooses to adjust item quantity)
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (isset($_POST['item_to_adjust']) &amp;&amp; $_POST['item_to_adjust'] != "") {
      // execute some code
      $item_to_adjust = $_POST['item_to_adjust'];
      $quantity = $_POST['quantity'];
      $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
      if ($quantity &gt;= 100) { $quantity = 99; }
      if ($quantity &lt; 1) { $quantity = 1; }
      if ($quantity == "") { $quantity = 1; }
      $i = 0;
      foreach ($_SESSION["cart_array"] as $each_item) { 
                $i++;
                while (list($key, $value) = each($each_item)) {
                    if ($key == "item_id" &amp;&amp; $value == $item_to_adjust) {
                        // That item is in cart already so let's adjust its quantity using array_splice()
                        array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" =&gt; $item_to_adjust, "quantity" =&gt; $quantity)));
                    } // close if condition
                } // close while loop
      } // close foreach loop
  }
  ?&gt;
  &lt;?php 
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //       Section 4 (if user wants to remove an item from cart)
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  if (isset($_POST['index_to_remove']) &amp;&amp; $_POST['index_to_remove'] != "") {
      // Access the array and run code to remove that array index
      $key_to_remove = $_POST['index_to_remove'];
      if (count($_SESSION["cart_array"]) &lt;= 1) {
          unset($_SESSION["cart_array"]);
      } else {
          unset($_SESSION["cart_array"]["$key_to_remove"]);
          sort($_SESSION["cart_array"]);
      }
  }
  ?&gt;
  &lt;?php 
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  //       Section 5  (render the cart for the user to view on the page)
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  $cartOutput = "";
  $cartTotal = "";
  $pp_checkout_btn = '';
  $product_id_array = '';
  if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) &lt; 1) {
      $cartOutput = "&lt;h2 align='center'&gt;Your shopping cart is empty&lt;/h2&gt;";
  } else {                  
      // Start PayPal Checkout Button
      $pp_checkout_btn .= '&lt;form action="https://www.paypal.com/cgi-bin/webscr" method="post"&gt;
      &lt;input type="hidden" name="cmd" value="_cart"&gt;
      &lt;input type="hidden" name="upload" value="1"&gt;
      &lt;input type="hidden" name="business" value="xxxxxx@gmail.com"&gt;';
      // Start the For Each loop
      $i = 0; 
      foreach ($_SESSION["cart_array"] as $each_item) { 
          $item_id = $each_item['item_id'];
          $sql = mysql_query("SELECT * FROM bag WHERE id='$item_id' LIMIT 1");
          while ($row = mysql_fetch_array($sql)) {
              $product_name = $row["title"];
              $price = $row["cprice"];
              $details = $row["description"];
              $image = $row["image"];
          }                                   
          $pricetotal = $price * $each_item['quantity'];
          $cartTotal = $pricetotal + $cartTotal;
          setlocale(LC_MONETARY, "en_US");
          $pricetotal = money_format("%10.2n", $pricetotal);
          // Dynamic Checkout Btn Assembly
          $x = $i + 1;
          $pp_checkout_btn .= '&lt;input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"&gt;
          &lt;input type="hidden" name="amount_' . $x . '" value="' . $price . '"&gt;
          &lt;input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"&gt;  ';
          // Create the product array variable
          $product_id_array .= "$item_id-".$each_item['quantity'].","; 
          // Dynamic table row assembly
          $cartOutput .= "&lt;tr&gt;";
          $cartOutput .= '&lt;td&gt;&lt;a href="product.php?id=' . $item_id . '"&gt;' . $product_name . '&lt;/a&gt;&lt;br /&gt;&lt;img src="admin/product/uploaded_files/' . $image . '" alt="' . $product_name. '" width="40" height="52" border="1" /&gt;&lt;/td&gt;';
          $cartOutput .= '&lt;td&gt;' . $details . '&lt;/td&gt;';
          $cartOutput .= '&lt;td&gt;$' . $price . '&lt;/td&gt;';
  
          $cartOutput .= '&lt;td&gt;&lt;form action="cart.php" method="post"&gt;
          &lt;input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /&gt;
          &lt;input name="adjustBtn' . $item_id . '" type="submit" value="change" /&gt;
          &lt;input name="item_to_adjust" type="hidden" value="' . $item_id . '" /&gt;
          &lt;/form&gt;&lt;/td&gt;';
  
      //  $cartOutput .= '&lt;td&gt;' . $each_item['quantity'] . '&lt;/td&gt;';
          $cartOutput .= '&lt;td&gt;' . $pricetotal . '&lt;/td&gt;';
          $cartOutput .= '&lt;td&gt;&lt;form action="cart.php" method="post"&gt;&lt;input name="deleteBtn' . $item_id . '" type="submit" value="X" /&gt;&lt;input name="index_to_remove" type="hidden" value="' . $i . '" /&gt;&lt;/form&gt;&lt;/td&gt;';
          $cartOutput .= '&lt;/tr&gt;';
          $i++; 
      } 
      setlocale(LC_MONETARY, "en_US");
      $cartTotal = money_format("%10.2n", $cartTotal);
      $cartTotal = "&lt;div style='font-size:18px; margin-top:12px;' align='right'&gt;Cart Total : ".$cartTotal." SGD&lt;/div&gt;";
      // Finish the Paypal Checkout Btn
      $pp_checkout_btn .= '&lt;input type="hidden" name="custom" value="' . $product_id_array . '"&gt;
      &lt;input type="hidden" name="notify_url" value="https://www.yoursite.com/storescripts/my_ipn.php"&gt;
      &lt;input type="hidden" name="return" value="https://www.yoursite.com/checkout_complete.php"&gt;
      &lt;input type="hidden" name="rm" value="2"&gt;
      &lt;input type="hidden" name="cbt" value="Return to The Store"&gt;
      &lt;input type="hidden" name="cancel_return" value="https://www.yoursite.com/paypal_cancel.php"&gt;
      &lt;input type="hidden" name="lc" value="SG"&gt;
      &lt;input type="hidden" name="currency_code" value="SGD"&gt;
      &lt;input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!"&gt;
      &lt;/form&gt;';
  }
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
      &lt;meta name="description" content=""&gt;
      &lt;meta name="author" content=""&gt;
      &lt;link rel="shortcut icon" href="../../assets/ico/favicon.png"&gt;
  
      &lt;title&gt;Homepage - Bagnista&lt;/title&gt;
  
      &lt;!-- Bootstrap core CSS --&gt;
      &lt;link href="css/bootstrap.css" rel="stylesheet"&gt;
  
  
      &lt;!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --&gt;
      &lt;!--[if lt IE 9]&gt;
        &lt;script src="../../assets/js/html5shiv.js"&gt;&lt;/script&gt;
        &lt;script src="../../assets/js/respond.min.js"&gt;&lt;/script&gt;
      &lt;![endif]--&gt;
  
  
    &lt;/head&gt;
  
    &lt;body&gt;
      &lt;?php 
  if (empty($_SESSION['user']))
   {
   include_once("header.php");
   }
  else     {
        include_once("header2.php");
        }?&gt;
  
  &lt;div class="page-header"&gt;
  
    &lt;div class="container"&gt;
  
  
      &lt;br /&gt;
      &lt;table width="100%" border="1" cellspacing="0" cellpadding="6"&gt;
        &lt;tr&gt;
          &lt;td width="18%" bgcolor="#C5DFFA"&gt;&lt;strong&gt;Product&lt;/strong&gt;&lt;/td&gt;
          &lt;td width="45%" bgcolor="#C5DFFA"&gt;&lt;strong&gt;Product Description&lt;/strong&gt;&lt;/td&gt;
          &lt;td width="10%" bgcolor="#C5DFFA"&gt;&lt;strong&gt;Unit Price&lt;/strong&gt;&lt;/td&gt;
          &lt;td width="9%" bgcolor="#C5DFFA"&gt;&lt;strong&gt;Quantity&lt;/strong&gt;&lt;/td&gt;
          &lt;td width="9%" bgcolor="#C5DFFA"&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
          &lt;td width="9%" bgcolor="#C5DFFA"&gt;&lt;strong&gt;Remove&lt;/strong&gt;&lt;/td&gt;
        &lt;/tr&gt;
       &lt;?php echo $cartOutput; ?&gt;
       &lt;!-- &lt;tr&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
          &lt;td&gt;&amp;nbsp;&lt;/td&gt;
        &lt;/tr&gt; --&gt;
      &lt;/table&gt;
      &lt;?php echo $cartTotal; ?&gt;
      &lt;br /&gt;
  &lt;br /&gt;
  &lt;?php echo $pp_checkout_btn; ?&gt;
      &lt;br /&gt;
      &lt;br /&gt;
       &lt;a href="cart.php?cmd=emptycart"&gt;&lt;button type="button" class="btn btn-warning"&gt;Click here to empty Shopping Cart!&lt;/button&lt;/a&gt;
  
  
  
      &lt;/div&gt;      
     &lt;br /&gt;     &lt;? echo  $pricetotal?&gt;   // code for debugging purpose
    &lt;/div&gt;
    &lt;?php include_once("footer.php");?&gt;
  &lt;? ob_flush(); ?&gt;        
  &lt;/html&gt;
  </code></pre>