shopping cart total

How to total a shopping cart

Page 1 of 1

1 Replies - 11685 Views - Last Post: 03 May 2009 - 09:05 AM Rate Topic: -----

#1 whizzell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 31-July 08

shopping cart total

Posted 27 April 2009 - 07:33 AM

I have a shopping cart online at 5.dvdreward.co.uk

As you can see if you go to products and add product the product is added to the cart but what i can't get to work is the total on the checkout.

The problem is that the data is coming from two recordsets. one with the order info in db named "cart" and the other from the tyres db.

I need to make an array i think of all the prices that are pulled from the tyres database so that i can use array sum. and get the total.

my code to create the cart so far is this. (on the add product page - a record is added to db cart for that tyre)

<?php require_once('Connections/remote.php'); ?>
<?php if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
	$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
	case "text":
	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
	  break;	
	case "long":
	case "int":
	  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
	  break;
	case "double":
	  $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
	  break;
	case "date":
	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
	  break;
	case "defined":
	  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
	  break;
  }
  return $theValue;
}
}

if ((isset($_POST['cartID'])) && ($_POST['cartID'] != "") && (isset($_POST['delete']))) {
  $deleteSQL = sprintf("DELETE FROM cart WHERE cartID=%s",
					   GetSQLValueString($_POST['cartID'], "int"));

  mysql_select_db($database_local, $local);
  $Result1 = mysql_query($deleteSQL, $local) or die(mysql_error());
  $deleteGoTo = "cart.php";
  if (isset($_SERVER['QUERY_STRING'])) {
	$deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
	$deleteGoTo .= $_SERVER['QUERY_STRING'];
  }

  header(sprintf("Location: %s", $deleteGoTo));

}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
	$response = "Your product was deleted from your cart.";

}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO cart (cartID, tyreID, ownerID, qty, `date`, `time`, `check`) VALUES (%s, %s, %s, %s, %s, %s, %s)",
					   GetSQLValueString($_POST['cartID'], "int"),
					   GetSQLValueString($_POST['tyreID'], "int"),
					   GetSQLValueString($_POST['ownerID'], "int"),
					   GetSQLValueString($_POST['qty'], "int"),
					   GetSQLValueString($_POST['date'], "date"),
					   GetSQLValueString($_POST['time'], "date"),
					   GetSQLValueString($_POST['check'], "int"));

  mysql_select_db($database_local, $local);
  $Result1 = mysql_query($insertSQL, $local) or die(mysql_error());

  $insertGoTo = "cart.php";
  if (isset($_SERVER['QUERY_STRING'])) {
 $response = "Your Product was added to your cart.";
  }
}?>

<?php 
mysql_select_db($database_local, $local);
$query_cart_items = "SELECT cartID, tyreID, ownerID, qty FROM cart WHERE ownerID = 1";
$cart_items = mysql_query($query_cart_items, $local) or die(mysql_error());
$row_cart_items = mysql_fetch_assoc($cart_items);
$totalRows_cart_items = mysql_num_rows($cart_items);


$queryString_cart_items = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
	if (stristr($param, "pageNum_cart_items") == false && 
		stristr($param, "totalRows_cart_items") == false) {
	  array_push($newParams, $param);
	}
  }
  if (count($newParams) != 0) {
	$queryString_cart_items = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_cart_items = sprintf("&totalRows_cart_items=%d%s", $totalRows_cart_items, $queryString_cart_items);


?>

<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr id="top">
		  <td width="5" colspan="1" id="left"><img src="/images/spacer.gif" alt="" width="5" height="1" /></td>
		  <td id="center"><p id="center2">Your Shopping Cart</p></td>
		  <td width="5" colspan="1" id="right"><img src="/images/spacer.gif" alt="" width="5" height="1" /></td>
		</tr>
		<tr id="content">
		  <td colspan="3">
		  <table width="100%" border="0" id="results" class="results" cellspacing="0" cellpadding="0">
		  <tr><td colspan="4"><?php echo $response;?></td></tr>
  <tr id="header">
	<td align="left">Product Description</td>
	<td align="left">Qty</td>
	<td align="left">Price</td>
	<td width="12%" align="left">Remove</td>
  </tr>
  <?php if ($totalRows_cart_items > 0) { // Show if recordset not empty ?>
  <?php do { ?>
<?php $tyreID_cart_tyre_detail = $row_cart_items['tyreID'];

mysql_select_db($database_local, $local);
$query_cart_tyre_detail = sprintf("SELECT * FROM tyres WHERE ID = %s", GetSQLValueString($tyreID_cart_tyre_detail, "int"));
$cart_tyre_detail = mysql_query($query_cart_tyre_detail, $local) or die(mysql_error());
$row_cart_tyre_detail = mysql_fetch_assoc($cart_tyre_detail);
$totalRows_cart_tyre_detail = mysql_num_rows($cart_tyre_detail); ?>
	<tr>
	  <td align="left">
		<table align="left"><tr><td colspan="2"><?php echo $row_cart_tyre_detail['brand']; ?><?php echo $row_cart_tyre_detail['model']; ?></td>
		  </tr>
		  <tr><td>size:</td><td><?php echo $row_cart_tyre_detail['tyre_w']; ?><?php echo $row_cart_tyre_detail['tyre_p']; ?><?php echo $row_cart_tyre_detail['wheel_s']; ?><?php echo $row_cart_tyre_detail['sr']; ?></td>
		  </table></td>
	  <td align="left"><?php echo $row_cart_items['qty']; ?></td>
	  <td align="left">
	  <?php $tip = $row_cart_items['qty'] * $row_cart_tyre_detail['price'];?>
	  <?php echo $tip;?>.00
	  <?php if (isset($oip)) $tpp = $oip + $tip;?>
	  <?php $tip = $oip;?>
	  </td>
	  <td align="left">
		<form id="delete" name="delete" method="post" action="">
		  <input type="hidden" value="del1" id="delete" name="delete"  />
		  <input type="hidden" id="cartID" name="cartID" value="<?php echo $row_cart_items['cartID']; ?>" />
		  <input type="image" src="images/x.gif" value="submit" id="submit" name="submit" /></form> </td>
	</tr>
	<?php } while ($row_cart_items = mysql_fetch_assoc($cart_items)); ?>
  <?php } // Show if recordset not empty ?>
  <?php if ($totalRows_cart_items == 0) { // Show if recordset empty ?>
	There are no items in your cart. click <a href="find_tyres.php">here</a> to order something.
  <?php } // Show if recordset empty ?>
<tr>
  <td colspan="4"><p>There is <?php if ($totalRows_cart_items > 1) $s = "s"; echo $totalRows_cart_items." item".$s;?> in your shopping cart.
  </p>
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
	  <tr>
	  <td width="2%"></td>
	  <td>Delivery: (inlcuded)</td>
	  <td><?php $value1 = 0;?><?php echo $value1;?>.00</td>
	</tr>
	<tr>
	  <td></td>
	  <td>Fitting: (included)</td>
	  <td><?php $value2 = 0;?><?php echo $value2;?>.00</td>
	</tr>
	<tr>
	  <td></td>
	  <td>Products total:</td>
	  <td><?php $value3 = $tpp;?><?php echo $value3;?>.00</td>
	</tr>
	<tr>
	  <td></td>
	  <td>Overall Total Cost</td>
	  <td><?php echo ($value1 + $value2 + $value3);?>.00</td>
	</tr>
  </table>
<table width="100%" cellpadding="0" cellspacing="0"><tr>
  <td colspan="2" height="10px"></td></tr><tr><td align="left"><a href="find_tyres.php"><img src="images/csb.gif" alt="Continue Shopping" width="150" height="51" border="0" /></a></td><td align="right"><a href="order.php"><img src="images/cob.gif" alt="Continue to Checkout" width="150" height="51" border="0" /></a></td></tr></table></td>
	</tr>
</table>




Is This A Good Question/Topic? 0
  • +

Replies To: shopping cart total

#2 inspectah  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 27-April 09

Re: shopping cart total

Posted 03 May 2009 - 09:05 AM

try this if it helps


function showCart() {
	$cart = $_SESSION['cart'];
	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
		$output[] = '<table>';
		foreach ($contents as $id=>$qty) {
			$sql = 'SELECT * FROM books WHERE id = '.$id;
			$result = $db->query($sql);
			$row = $result->fetch();
			extract($row);
			$output[] = '<tr>';
			$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
			$output[] = '<td>'.$title.' by '.$author.'</td>';
			$output[] = '<td>&pound;'.$price.'</td>';
			$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
			$output[] = '<td>&pound;'.($price * $qty).'</td>';
			$total += $price * $qty;
			$output[] = '</tr>';
		}
		$output[] = '</table>';
		$output[] = '<p>Grand total: &pound;'.$total.'</p>';
		$output[] = '<div><button type="submit">Update cart</button></div>';
		$output[] = '</form>';
	} else {
		$output[] = '<p>You shopping cart is empty.</p>';
	}
	return join('',$output);
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1