Adding 2 Variables for Total

I have a handy cart but trying to get the subtotal + activation ($25) + shipping fee for the grand total. If anyone could help I would be VERY grateful! Here’s the code:

[code]<?php session_start(); ?>

TV Connect.US Order Check Out <?php if(isset($_GET["id"])) { if($_SESSION["cnt"]>0) { $_SESSION["cntqty"] = $_SESSION["cntqty"] - $_SESSION["qty".$_GET["id"]]; for ( $counter = $_GET["id"]; $counter < $_SESSION["cnt"]; $counter += 1) { $incr = $counter+1; $_SESSION["title".$counter] = $_SESSION["title".$incr]; $_SESSION["paymentmethod".$counter] = $_SESSION["paymentmethod".$incr]; $_SESSION["price".$counter] = $_SESSION["price".$incr]; $_SESSION["qty".$counter] = $_SESSION["qty".$incr]; $_SESSION["total".$counter] = $_SESSION["total".$incr]; } $_SESSION["cnt"] = $_SESSION["cnt"]-1; $_SESSION["grandtotal"] = $_SESSION["grandtotal"] - $_SESSION["total".$_GET["id"]];
		}
	
	}
	else if(isset($_GET["prod"]))
	{
		$_SESSION["cnt"] = $_SESSION["cnt"]+1;
		$_SESSION["title".$_SESSION["cnt"]] = $_GET["prod"];
		$_SESSION["paymentmethod".$_SESSION["cnt"]] = $_GET["pmode"];
		$_SESSION["price".$_SESSION["cnt"]] = $_GET["price"];
		$_SESSION["qty".$_SESSION["cnt"]] = $_GET["qty"];
		$_SESSION["total".$_SESSION["cnt"]] = $_GET["price"]*$_GET["qty"];
		$_SESSION["grandtotal"] = $_SESSION["paymentmethod"] + $_SESSION["grandtotal"] + $_SESSION["total".$_SESSION["cnt"]]; 
		
	} ?>
	<? $_SESSION['discount'] = $discount; ?>
<? $_SESSION['reducedTotal'] = $reducedTotal; ?>

<style type="text/css">

/*Credits: Dynamic Drive CSS Library */
/*URL: http://www.dynamicdrive.com/style/ */

.thumbnail{
position: relative;
z-index: 0;
}

.thumbnail:hover{
background-color: transparent; border:none;
z-index: 50;
}

.thumbnail span{ /CSS for enlarged image/
position: absolute;
background-color: lightyellow;
padding: 6px;
left: -1000px;
border: 1px dashed gray;
visibility: hidden;
color: black;
text-decoration: none;
}

.thumbnail span { /CSS for enlarged image/
border-width: 0;
padding: 2px;
}

.thumbnail:hover span{ /CSS for enlarged image on hover/
visibility: visible;
top: 0;
left: 60px; /*position where enlarged image should offset horizontally */

}

</style>
Javascript must be enabled for this site to function properly.
Please enable it and reload your browser (F5).

<?php for ( $counter = 1; $counter <= $_SESSION["cnt"]; $counter += 1) { if(($counter%2)==0) { ?> <?php } ?> <?php } ?>
            <tr>
              <td colspan="6" align="right" valign="middle" background="images/base.png" style="height:2px; padding:0px" ></td>
            </tr>
            
            <tr>
              <td colspan="4" align="right" valign="middle" bgcolor="#1F1F1F" class="tableborder"><span class="style51">Sub Total:</span></td>
              <td valign="middle" bgcolor="#1F1F1F" class="tableborder" style="padding-left:6px">$<?php echo($_SESSION["grandtotal"]); ?></td>
              <td valign="middle" bgcolor="#1F1F1F" class="tableborder" style="padding-left:6px">&nbsp;</td>
            </tr>
            <tr>
              <td colspan="4" align="right" valign="middle" bgcolor="#373737" class="tableborder">Activation:</td>
              <td valign="middle" bgcolor="#373737" class="tableborder">$25</td>
              <td valign="middle" bgcolor="#373737" class="tableborder">&nbsp;</td>
            </tr>
            
            
            <tr>
              <td colspan="4" align="right" valign="middle" bgcolor="#1F1F1F" class="tableborder"><span class="style51">Grand Total: </span>
                </strong></td>
              <td valign="middle" bgcolor="#1F1F1F" class="tableborder" style="text-align:left;"><? echo($_SESSION["grandTotalToRuleThemAll"]); ?></td>
              <td valign="middle" bgcolor="#1F1F1F" class="tableborder" style="text-align:left; padding-left:6px">&nbsp;</td>
            </tr>
          </table>
      </form>[/code]
     

 Click Here to continue shopping.
      Do NOT hit the Back button as this can cause an error.


Your Order Cart Details

Product Price Qty Shipping Total Remove
<?php echo($_SESSION["title".$counter]); ?> $<?php echo($_SESSION["price".$counter]); ?> <?php echo($_SESSION["qty".$counter]); ?>   $<?php echo($_SESSION["total".$counter]); ?> If your cart item won't empty this item please simply close the browser and re-open tvconnect.us.
Shipping: $<?php echo($_SESSION["paymentmethod".$counter]); ?>  

Not sure if I am understanding. Just total them up… You are storing everything in the SESSION array, so just add those and display them.

I hope this code is not going to be used for real and is just a programming class project? You should NEVER pass prices through the browser, since they can be set to any value anyone wants. You will end up selling things and shipping them for zero amounts.

You should submit the minimum amount of data from the browser and you should validate all data before using it. For the add to cart code (it would help if you commented the code so that anyone reading it can tell what it is trying to do), you should only submit the product id and the quantity. The cart should use the product id as the cart array’s index and the value in the cart element should be the quantity. Using this cart definition will eliminate most of the logic you have now for the add to cart and delete from cart code.

For the shipping method (which is not the payment method that the code is naming the variables for), you should only submit the shipping method id and store this in a session variable, separate from the cart array.

When you display the cart, you should get the product id’s from the cart (see php’s array_keys()), query to get the product information from where it is stored, then loop over the query results to display the cart, getting the quantity values from the cart. You would multiply the quantity and the price to get the total for each product to display and to add to the grand total. When you display money amounts, you should use number_format().

The activation fee should be defined in a configuration file or a configuration database table, then referenced at the appropriate point in the code, so that it can easily be modified by changing configuration data, rather than to try to find every place it gets used in the code. You would display the activation fee and add it to the variable holding the grand total.

To display the shipping amount, you would take the stored shipping method id and apply the rules that produce the shipping cost, then display the amount and add it to the variable holding the grand total.

Then just display the grand total amount at the appropriate point.

Edit: if you use css properly to style the page, the html will be greatly simplified, reducing the clutter and make it easier to change the code and the styling.

You should also use post method forms for the add to cart and delete from cart operations.

Sponsor our Newsletter | Privacy Policy | Terms of Service