...

View Full Version : Shopping cart logic



graham23s
03-08-2008, 10:54 PM
Hi Guys,

my shopping cart logic seems toi be a wee bit off, the first 2 vars:

$product_id and $quantity are coming from the "add to cart" form. can i improve the top half of the code mainly just after i grab the GET's i think i may have over complicated it, the bottom half updating and deleting works great, just the top half seems to not add some items but does others.


<?php
// standard header //
print("<div class=\"subheader\"><div id=\"title\">Home > <span class=\"blue\">Items you have added to your shopping cart</span></div>You can delete and update your shopping cart as you wish.</div>");

// check for the users logged in session if there is none no items in the cart //
if(!isset($_SESSION['id']))
{
print("<div id=\"shopping_login_error\">We are sorry but you need to be logged in to add orders. you can login <a href=\"login.php\">here</a> or register <a href=\"register.php\">here</a>.");
include("inc/footer.php");
exit;
}

// they are logged in so insert and display the cart items //
// firstly insert the items into the database //

// get vars //
$product_id = $_GET['product_id'];
$quantity = $_GET['q'];

echo "(<b>$product_id</b>) (<b>$quantity</b>)";

// session var //
$var_loggedinuserid = $_SESSION['id'];

// do some checking on the gets //
//if(!isset($product_id) || !isset($quantity))
//{
// display_error("Oops, an errors had occured.");
//}

// do a quick check to see if the product has already been added if so just update the quantity //
$query_already_inserted = "SELECT `product_id`,`quantity` FROM `fcp_orders` WHERE `product_id`='$product_id'";
$results_already_inserted = mysql_query($query_already_inserted);
echo $query_already_inserted;
// already in the cart ? //
$already_in_cart = mysql_num_rows($results_already_inserted);

// do the math //
$row_quantity = mysql_fetch_array($results_already_inserted);

// current quantity //
$cur_qty = $row_quantity['quantity'];

// new quantity //
$new_quantity = $cur_qty + $quantity;

if($already_in_cart > 0)
{

// just update the quantity //
$queryqty = mysql_query("UPDATE `fcp_orders` SET `quantity`='$new_quantity' WHERE `product_id`='$product_id'");

} else {

//if($product_id == 0)
//{

//

//} else {

// insert the products into mysql //
$query_insertion = "INSERT INTO `fcp_orders` (`id`,`customer_id`,`product_id`,`quantity`,`date`) VALUES ('','$var_loggedinuserid','$product_id','$quantity',now())";
$results_insertion = mysql_query($query_insertion);
echo $query_insertion;
// do a quick javascript refresh //
//print('<script type="text/javascript">window.location = "shoppingcart.php"</script>');

// }

}

// now query the database to see what the logged in user has added //
$query_cart = "SELECT * FROM `fcp_orders` WHERE `customer_id`='$var_loggedinuserid'";
$result_cart = mysql_query($query_cart);

// any results back //
$num_products = mysql_num_rows($result_cart);

// display if there is or isn't //
if($num_products == 0)
{

print("<div align=\"left\" id=\"empty_shopping_cart\">Your shopping cart is empty. <img src=\"images/cartempty.gif\" alt=\"Your shopping cart is empty!\"></div>");
include("inc/footer.php");
exit;

} else {

// -- EMPTY -- //
if($_GET['action'] == 'empty')
{
$customers_session_id = $_SESSION['id'];

// delete the data from mysql //
$query_empty_cart = mysql_query("DELETE FROM `fcp_orders` WHERE `customer_id`='$customers_session_id'");

// do a quick javascript refresh //
print('<script type="text/javascript">window.location = "shoppingcart.php"</script>');
}

// -- DELETE -- //
if($_GET['action'] == 'delete')
{
// grab the vars //
$customers_product_id = $_GET['productid'];
$customers_session_id = $_SESSION['id'];

// now we have the product id delete from mysql where product id is product id and logged in session id is customer id //
$query_delete_cart = mysql_query("DELETE FROM `fcp_orders` WHERE `product_id`='$customers_product_id' AND `customer_id`='$customers_session_id'");

// do a quick javascript refresh //
print('<script type="text/javascript">window.location = "shoppingcart.php"</script>');

}

// -- UPDATE -- //
if($_GET['action'] == 'update')
{

$product_id_to_update = $_POST['pid'];
$quantity_to_update = $_POST['q'];

foreach ($_POST['pid'] as $k => $prod)
{
$qty = $_POST['q'][$k];

$sql = mysql_query("UPDATE `fcp_orders` SET `quantity`='$qty' WHERE `product_id`='$prod'");

// do a quick javascript refresh //
print('<script type="text/javascript">window.location = "shoppingcart.php"</script>');

}

}

// there is products there so display them //
print("<form action=\"shoppingcart.php?action=update\" method=\"post\">");

print("<table width=\"95&#37;\" class=\"shop_table\" border=\"1\" bordercolor=\"#3399CC\" cellpadding=\"5\" cellspacing=\"0\">\n");
print("<tr>\n");
print("<td align=\"center\" class=\"shop_header\">Image</td><td align=\"center\" class=\"shop_header\">Product</td><td align=\"center\" class=\"shop_header\" >Quantity</td><td align=\"center\" class=\"shop_header\">Total Price</td><td align=\"center\" class=\"shop_header\">Remove</td>\n");
print("</tr>\n");
print("<tr>\n");

// loop //
while($row = mysql_fetch_array($result_cart))
{

$product_id = $row['product_id'];
$product_qty = $row['quantity'];

// get the product information //
$query_product_information = "SELECT * FROM `fcp_products` WHERE `id`='$product_id'";
$result_product_information = mysql_query($query_product_information);

// make an array //
$rows = mysql_fetch_array($result_product_information);

// more vars //
$product_name = $rows['product_name'];
$product_price = $rows['product_price'];

// math //
$total_price = $product_price * $product_qty;

// number format //
$total_price = number_format($total_price,2);

// session in a var //
$session_id = $_SESSION['id'];

// put the total quantity in mysql besides the order //
$finalupdate = mysql_query("UPDATE `fcp_orders` SET `quantity_total`='$total_price' WHERE `product_id`='$product_id'");

print("<td width=\"5%\" align=\"center\"><img src=\"images/image.gif\" alt=\"Product Image\" title=\"Product Image\" /></td><td align=\"center\"><a class='smart_links' href=\"productinformation.php?productid=$product_id\">$product_name</a><br />You have ordered <b>$product_qty</b> at &pound;$product_price each.</td><td align=\"center\"><input type=\"hidden\" name=\"pid[]\" value=\"$product_id\"><input type=\"text\" name=\"q[]\" size=\"5\" value=\"$product_qty\"></td><td width=\"10%\" align=\"center\">&pound;$total_price</td><td align=\"center\" width=\"5%\"><a href=\"shoppingcart.php?productid=$product_id&customerid=$session_id&action=delete\"><img src=\"images/button_delete.gif\" border=\"0\"></a></td></tr>\n");

}

// get the total price due //
$query_price = "SELECT SUM(quantity_total) as `total` FROM `fcp_orders` WHERE `customer_id`='$session_id'";
$results_price = mysql_query($query_price);
$r = mysql_fetch_array($results_price);

// vars //
$total_due = $r['total'];

// shipping //
$shipping_costs = 10;

// total owed [make this a function] //
$total_owed = $total_due + $shipping_costs;

// number format //
$total_owed = number_format($total_owed, 2);
$total_due = number_format($total_due, 2);

// end the table //
print("<tr>");
print("<td colspan=\"4\" align=\"right\">empty your shopping cart <a href=\"shoppingcart.php?action=empty\" onclick=\"return confirm('You sure you want to empty your cart?')\"><img src=\"images/cross.gif\" border=\"0\"></a></td><td align=\"right\"><input type=\"submit\" name=\"submit\" class=\"add_to_cart_button\" value=\"Update Cart\"></td>");
print("</tr>");
print("<tr>");
print("<td colspan=\"4\" align=\"right\">Sub Total -</td><td align=\"left\"><b>&pound;$total_due</b></td>");
print("</tr>");
print("<tr>");
print("<td colspan=\"4\" align=\"right\">Shipping -</td><td align=\"left\"><b>&pound;$shipping_costs</b></td>");
print("</tr>");
print("<tr>");
print("<td colspan=\"4\" align=\"right\">Total -</td><td align=\"left\"><b>&pound;$total_owed</b></td>");
print("</tr>");
print("</table></form>");

// buttons //
print("<form action=\"checkout.php\" method=\"post\">");
print("<table width=\"95%\" border=\"0\">");
print("<tr>");
print("<td align=center><img src=\"images/cards.bmp\" alt=\"Payments we accept!\"></td>");
print("<td align=center><input type=\"submit\" value=\"Proceed to Checkout\" style=\"font-weight: bold; font-size: 120%;\"></td>");
print("</table>");

print("</form>");

//}

} // end quantity else //
/*
// this code deletes orders from the database older than 1 day //
$yesterday = date('Y-m-d H:i:s',mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
$delete_cart_entries = mysql_query("DELETE FROM `fcp_orders` WHERE `date` < '$yesterday'");
*/
?>

cheers

Graham



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum