ultraloveninja Posted February 21, 2010 Share Posted February 21, 2010 Hey there. I am adding some checking into a function for a shopping cart and I can sorta get it to work, but I am not sure if it's in the right place and/or formatted correctly. I kinda need a fresh set of eye to take a look at it cause mine are getting a little batty going through all the case statements. Basically it's checking to see if there are more than 4 items in the cart and then lets the user know if there are more than 4 items in the cart and that they need to change it. I can get it to work where it shows the notification but it only works if I update the cart twice and it still allows the user to proceed to checkout. So I am not sure if I have put it in the right place in the function. function updateCart() { $cartId = $_POST['hidCartId']; $productId = $_POST['hidProductId']; $itemQty = $_POST['txtQty']; $numItem = count($itemQty); $numDeleted = 0; $notice = ''; for ($i = 0; $i < $numItem; $i++) { $newQty = (int)$itemQty[$i]; if ($newQty < 1) { // remove this item from shopping cart deleteFromCart($cartId[$i]); $numDeleted += 1; }else { // check current stock $sql = "SELECT pd_name, pd_qty FROM tbl_product WHERE pd_id = {$productId[$i]}"; $result = dbQuery($sql); $row = dbFetchAssoc($result); if ($newQty > $row['pd_qty']) { // we only have this much in stock $newQty = $row['pd_qty']; // if the customer put more than // we have in stock, give a notice if ($row['pd_qty'] > 0) { setError('The quantity you have requested is more than we currently have in stock. The number available is indicated in the "Quantity" box. '); } else { // the product is no longer in stock setError('Sorry, but the product you want (' . $row['pd_name'] . ') is no longer in stock'); // remove this item from shopping cart deleteFromCart($cartId[$i]); $numDeleted += 1; } } else { //Here's the new code that I added: $sql = "select ct_qty from tbl_cart"; $result = mysql_query($sql); $total_count = 0; while ($row = mysql_fetch_array($result)) { $total_count += $row['ct_qty']; } if ($total_count > 4) { setError('You can only order up to 4 pies in one order. '); } } // update product quantity $sql = "UPDATE tbl_cart SET ct_qty = $newQty WHERE ct_id = {$cartId[$i]}"; dbQuery($sql); } } if ($numDeleted == $numItem) { // if all item deleted return to the last page that // the customer visited before going to shopping cart header("Location: $returnUrl" . $_SESSION['shop_return_url']); } else { header('Location: cart.php'); } exit; } Thanks for any help! Link to comment https://forums.phpfreaks.com/topic/192859-correct-formatting-in-function/ Share on other sites More sharing options...
ialsoagree Posted February 21, 2010 Share Posted February 21, 2010 I haven't thoroughly looked at your code but noticed something that might be relevant. $itemQty = $_POST['txtQty']; $numItem = count($itemQty); Is gettype($_POST['txtQty') == 'array'? Or is it == 'object'? If not, you can't run count() on $itemQty because it's not an array or object. If it is an array or object, well then just ignore me. It's a bit unclear in the initial code what variable type you're storing there (the name implies text but the function used implies an array or an object). Link to comment https://forums.phpfreaks.com/topic/192859-correct-formatting-in-function/#findComment-1015825 Share on other sites More sharing options...
ultraloveninja Posted February 21, 2010 Author Share Posted February 21, 2010 HA! Good question. Well here is what I can tell you. I am using plaincart mainly because I need a basic shopping cart to handle a TON of custom stuff with shipping calculations and what not. The function that you see populates another page (cart.php) listed here: <form action="<?php echo $_SERVER['PHP_SELF'] . "?action=update"; ?>" method="post" name="frmCart" id="frmCart"> <table width="780" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable"> <tr class="entryTableHeader"> <td colspan="2" align="center">Item</td> <td align="center">Unit Price</td> <td width="75" align="center">Quantity</td> <td align="center">Total</td> <td width="75" align="center"> </td> </tr> <?php $subTotal = 0; for ($i = 0; $i < $numItem; $i++) { extract($cartContent[$i]); $productUrl = "index.php?c=$cat_id&p=$pd_id"; $subTotal += $pd_price * $ct_qty; ?> <tr class="content"> <td width="80" align="center"><a href="<?php echo $productUrl; ?>"><img src="<?php echo $pd_thumbnail; ?>" border="0"></a></td> <td><a href="<?php echo $productUrl; ?>"><?php echo $pd_name; ?></a></td> <td align="right"><?php echo displayAmount($pd_price); ?></td> <td width="75"><input name="txtQty[]" type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);"> <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>"> <input name="hidProductId[]" type="hidden" value="<?php echo $pd_id; ?>"> </td><?php if ($ct_qty == 2) { echo displayAmount($pd_price * $ct_qty - 2); } else { echo displayAmount($pd_price * $ct_qty); } ?> <td align="right"> </td> <td width="75" align="center"> <input name="btnDelete" type="button" id="btnDelete" value="Delete" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=delete&cid=$ct_id"; ?>';" class="box"> </td> </tr> <?php } ?> <tr class="content"> <td colspan="4" align="right">Sub-total</td> <td align="right"><?php echo displayAmount($subTotal); ?></td> <td width="75" align="center"> </td> </tr> <tr class="content"> <td colspan="4" align="right">Shipping and Handling</td> <td align="right"><?php echo displayAmount($shopConfig['shippingCost']); ?></td> <td width="75" align="center"> </td> </tr> <tr class="content"> <td colspan="4" align="right">Total </td> <td align="right"><?php echo displayAmount($subTotal + $shopConfig['shippingCost']); ?></td> <td width="75" align="center"> </td> </tr> <tr class="content"> <td colspan="5" align="right"> </td> <td width="75" align="center"> <input name="btnUpdate" type="submit" id="btnUpdate" value="Update Cart" class="box"></td> </tr> </table> </form> From the looks of it, it appears that the "txtQty[]" is an object? Not too sure how it operates (FYI - My PHP skills are not that great. Most of it comes from trial and error), but I do know that it pulls the item quantity from the database and then populates it into a field and the user then can update the quantity in the field before checking out I just need to check to make sure that the total overall quantity is not more than 4 for all items that are in the cart. I hope that makes some sense? Link to comment https://forums.phpfreaks.com/topic/192859-correct-formatting-in-function/#findComment-1015834 Share on other sites More sharing options...
ultraloveninja Posted February 22, 2010 Author Share Posted February 22, 2010 Here's the cart functions as a whole: <?php /********************************************************* * SHOPPING CART FUNCTIONS *********************************************************/ function addToCart() { // make sure the product id exist if (isset($_GET['p']) && (int)$_GET['p'] > 0) { $productId = (int)$_GET['p']; } else { header('Location: index.php'); } // does the product exist ? $sql = "SELECT pd_id, pd_qty FROM tbl_product WHERE pd_id = $productId"; $result = dbQuery($sql); if (dbNumRows($result) != 1) { // the product doesn't exist header('Location: cart.php'); } else { // how many of this product we // have in stock $row = dbFetchAssoc($result); $currentStock = $row['pd_qty']; if ($currentStock == 0) { // we no longer have this product in stock // show the error message setError('The product you requested is no longer in stock'); header('Location: cart.php'); exit; } } // current session id $sid = session_id(); // check if the product is already // in cart table for this session $sql = "SELECT pd_id FROM tbl_cart WHERE pd_id = $productId AND ct_session_id = '$sid'"; $result = dbQuery($sql); if (dbNumRows($result) == 0) { // put the product in cart table $sql = "INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ct_date) VALUES ($productId, 1, '$sid', NOW())"; $result = dbQuery($sql); } else { // update product quantity in cart table $sql = "UPDATE tbl_cart SET ct_qty = ct_qty + 1 WHERE ct_session_id = '$sid' AND pd_id = $productId"; $result = dbQuery($sql); } // an extra job for us here is to remove abandoned carts. // right now the best option is to call this function here deleteAbandonedCart(); header('Location: ' . $_SESSION['shop_return_url']); } /* Get all item in current session from shopping cart table */ function getCartContent() { $cartContent = array(); $sid = session_id(); $sql = "SELECT ct_id, ct.pd_id, ct_qty, pd_name, pd_price, pd_thumbnail, pd.cat_id FROM tbl_cart ct, tbl_product pd, tbl_category cat WHERE ct_session_id = '$sid' AND ct.pd_id = pd.pd_id AND cat.cat_id = pd.cat_id"; $result = dbQuery($sql); while ($row = dbFetchAssoc($result)) { if ($row['pd_thumbnail']) { $row['pd_thumbnail'] = WEB_ROOT . 'images/product/' . $row['pd_thumbnail']; } else { $row['pd_thumbnail'] = WEB_ROOT . 'images/no-image-small.png'; } $cartContent[] = $row; } return $cartContent; } /* Remove an item from the cart */ function deleteFromCart($cartId = 0) { if (!$cartId && isset($_GET['cid']) && (int)$_GET['cid'] > 0) { $cartId = (int)$_GET['cid']; } if ($cartId) { $sql = "DELETE FROM tbl_cart WHERE ct_id = $cartId"; $result = dbQuery($sql); } header('Location: cart.php'); } /* Update item quantity in shopping cart */ function updateCart() { $cartId = $_POST['hidCartId']; $productId = $_POST['hidProductId']; $itemQty = $_POST['txtQty']; $numItem = count($itemQty); $numDeleted = 0; $notice = ''; for ($i = 0; $i < $numItem; $i++) { $newQty = (int)$itemQty[$i]; if ($newQty < 1) { // remove this item from shopping cart deleteFromCart($cartId[$i]); $numDeleted += 1; }else { // check current stock $sql = "SELECT pd_name, pd_qty FROM tbl_product WHERE pd_id = {$productId[$i]}"; $result = dbQuery($sql); $row = dbFetchAssoc($result); if ($newQty > $row['pd_qty']) { // we only have this much in stock $newQty = $row['pd_qty']; // if the customer put more than // we have in stock, give a notice if ($row['pd_qty'] > 0) { setError('The quantity you have requested is more than we currently have in stock. The number available is indicated in the "Quantity" box. '); } else { // the product is no longer in stock setError('Sorry, but the product you want (' . $row['pd_name'] . ') is no longer in stock'); // remove this item from shopping cart deleteFromCart($cartId[$i]); $numDeleted += 1; } } // update product quantity $sql = "UPDATE tbl_cart SET ct_qty = $newQty WHERE ct_id = {$cartId[$i]}"; dbQuery($sql); } } if ($numDeleted == $numItem) { // if all item deleted return to the last page that // the customer visited before going to shopping cart header("Location: $returnUrl" . $_SESSION['shop_return_url']); } else { header('Location: cart.php'); } exit; } function isCartEmpty() { $isEmpty = false; $sid = session_id(); $sql = "SELECT ct_id FROM tbl_cart ct WHERE ct_session_id = '$sid'"; $result = dbQuery($sql); if (dbNumRows($result) == 0) { $isEmpty = true; } return $isEmpty; } /* Delete all cart entries older than one day */ function deleteAbandonedCart() { $yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y'))); $sql = "DELETE FROM tbl_cart WHERE ct_date < '$yesterday'"; dbQuery($sql); } ?> Link to comment https://forums.phpfreaks.com/topic/192859-correct-formatting-in-function/#findComment-1015920 Share on other sites More sharing options...
ultraloveninja Posted February 22, 2010 Author Share Posted February 22, 2010 I am wondering if i need to put it in its own function????? Link to comment https://forums.phpfreaks.com/topic/192859-correct-formatting-in-function/#findComment-1016426 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.