Forum Moderators: coopster
<?php
$priceTotal="";
$product_name="";
$price="";
$details="";
$cartOutput="";
$cartTotal="";
if(!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"])<1){
$cartOutput="<h2 align='center'>your cart is empty</h2>";
}else{
//index starts at 0
$i=0;
foreach($_SESSION["cart_array"]as $each_item){
$item_id=$each_item['item_id'];
$sql=mysql_query("SELECT * FROM products WHERE id='$item_id'LIMIT 1");
while($row=mysql_fetch_array($sql)){
$product_name=$row["product_name"];
$price=$row["price"];
$details=$row["details"];
}
$pricetotal=$price * $each_item['quantity'];
$cartTotal=$pricetotal + $cartTotal;
//formats currency
setlocale(LC_MONETARY, "en_GB");
$pricetotal=money_format("%!10.2n",$pricetotal);
//Dynamic tabel starts here
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '"></a><br/><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name . '" width="117" height="178" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>£' . $price . ' </td>';
$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>£' . $pricetotal . '</td>';
$cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '"type="submit" value="x"/><input name="item_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .='</tr>';
//items add by 1
$i++;
}
setlocale(LC_MONETARY, "en_GB");
$cartTotal=money_format("%!10.2n",$cartTotal);
$cartTotal="<div align='right'>Cart Total:£" .$cartTotal. " </div>";
}
?>
<?php echo $cartOutput ?>
</table>
<table width="400" border="1">
<tr>
<td width="134" align="right" bgcolor="#666666"><a href="cart.php?cmd=emptycart"> Empty Cart </a></td>
<td width="150" align="left" bgcolor="#666666"><?php echo $cartTotal ?></td
>
<td width="150" align="left" bgcolor="#666666"><form id="checkBtn" name="checkBtn" method="post" action="cartsubmit.php">
<input type="submit" name="button" id="button" value="Submit" />
</form></td
>
<form name="cart" method="post" action="checkout.php" >
<p class="center">SHOPPING CART</p>
<p>No of Items: <strong></strong>
<br/>Total Price:<strong>0.00</strong>
<br>Product ID: <strong></strong><br/>
</p>
<p class="button"><input type="submit" name="Submit" value=" Check Out "></form>
<?php
session_start();
$_SESSION["valid_id"];
$_SESSION["cost"];
$_SESSION["products"];
$_SESSION["order"];
$items = explode(",",$_SESSION["order"]);
include ("dbConfig.php");
include ("cart.php");
$sql="SELECT * FROM products";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
for( $i = 0; $i < count($items); ++$i )
{
if ($items[$i]==$row['product_id'])
{
$order = $order.$row["title"].",";
}
}
}
$sql= "INSERT INTO `orders` (`customer_id`, `order_details`, `total_price`)"
."VALUES ('".$_SESSION["valid_id"]."', "
."'".$_SESSION["order"]."', "
."'".$_SESSION["cost"]."')";
mysql_query($sql);
$_SESSION["cost"]=0;
$_SESSION["products"]=0;
$_SESSION["order"]="";
header ('Location: thanks.php?order=true');