Jump to content

ca99uk

Members
  • Posts

    11
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

ca99uk's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Having looked into a bit more I dont think the variables are being passed correctly. On the page where the update cart button is the variables are setup like: <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>"> <input name="hidProductId[]" type="hidden" value="<?php echo $pd_id; ?>"> and the variables are handled by the function like: 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; } if I try to print the array I get nothing. Anyone have any ideas? Thanks.
  2. Hello Everyone, I am using a script to try and setup an online shop everything is working great but on the cart the update button for changing the quantity is not working and I am really struggling to see what is wrong. Here is the Cart.php page where the form is <?php require_once 'library/config.php'; require_once 'library/cart-functions.php'; $action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view'; switch ($action) { case 'add' : addToCart(); break; case 'update' : updateCart(); break; case 'delete' : deleteFromCart(); break; case 'view' : } $cartContent = getCartContent(); $numItem = count($cartContent); $pageTitle = 'Shopping Cart'; require_once 'include/header.php'; // show the error message ( if we have any ) displayError(); if ($numItem > 0 ) { ?> <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> <td align="right"><?php echo displayAmount($pd_price * $ct_qty); ?></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 </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> <?php } else { ?> <p> </p><table width="550" border="0" align="center" cellpadding="10" cellspacing="0"> <tr> <td><p align="center">You shopping cart is empty</p> <p>If you find you are unable to add anything to your cart, please ensure that your internet browser has cookies enabled and that any other security software is not blocking your shopping session.</p></td> </tr> </table> <?php } $shoppingReturnUrl = isset($_SESSION['shop_return_url']) ? $_SESSION['shop_return_url'] : 'index.php'; ?> <table width="550" border="0" align="center" cellpadding="10" cellspacing="0"> <tr align="center"> <td><input name="btnContinue" type="button" id="btnContinue" value="<< Continue Shopping" onClick="window.location.href='<?php echo $shoppingReturnUrl; ?>';" class="box"></td> <?php if ($numItem > 0) { ?> <td><input name="btnCheckout" type="button" id="btnCheckout" value="Proceed To Checkout >>" onClick="window.location.href='checkout.php?step=1';" class="box"></td> <?php } ?> </tr> </table> <?php require_once 'include/footer.php'; ?> and this is the cart-functions.php with the updateCart function <?php require_once 'config.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 hello() { echo "test"; } 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); } ?> Any help is much appreciated thank you.
  3. Hi Jacko_162, I found this post really useful as I was looking to do a similar thing, however because of the large import I keep getting a timeout error on the execution did you manage to get a workaround to this?
  4. Hi all, seeing as getting no where with this im guessing it must be kind of impossible to achieve what I want with this function. So if I abandon that idea is there anyway to modify the code so it just gives a list of all the categories in the select box? Any help would be appreciated. Thank you very much
  5. Friendly bump am still stuck with this please any php wiz freaks out there I need your help
  6. Hello everyone, I am currently working on the script taken from www.phpwebcommerce.com I am only a beginner and have been using it in a little project for an online game just to help my mates be able to trade items and such. I have managed to get almost everything sorted apart from one thing. The function which gets the categories for a drop down select box: /* Generate combo box options containing the categories we have. if $catId is set then that category is selected */ function buildCategoryOptions($catId = 0) { $sql = "SELECT cat_id, cat_parent_id, cat_name FROM tbl_category ORDER BY cat_id"; $result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error()); $categories = array(); while($row = dbFetchArray($result)) { list($id, $parentId, $name) = $row; if ($parentId == 0) { // we create a new array for each top level categories $categories[$id] = array('name' => $name, 'children' => array()); } else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); } } // build combo box options $list = ''; foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; $list .= "<optgroup label=\"$name\">"; foreach ($children as $child) { $list .= "<option value=\"{$child['id']}\""; if ($child['id'] == $catId) { $list.= " selected"; } $list .= ">{$child['name']}</option>\r\n"; } $list .= "</optgroup>"; } return $list; } Basically the problem is that it only creates a category list of two levels e.g. Parent Child and I wish to be able to have more sub categories e.g. Parent Child Child Child Is there anyway of adapting this code so it can do something like that? Many thanks for any help.
  7. Friendly bump... please help. Thank you
  8. Hi Everyone, I dont know if this is possible or impossible .... is it worth trying to figure out how to adapt the function to do more than two category levels or is a total rewrite needed? I honestly thought it would be quite straight forward but obviously does not seem so.. any pointer would be great. Thanks.
  9. Hi Everyone, been looking and still cant figure it out if anyone has any ideas greatly appreciated /* Generate combo box options containing the categories we have. if $catId is set then that category is selected */ function buildCategoryOptions($catId = 0) { $sql = "SELECT cat_id, cat_parent_id, cat_name FROM tbl_category ORDER BY cat_id"; $result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error()); $categories = array(); while($row = dbFetchArray($result)) { list($id, $parentId, $name) = $row; if ($parentId == 0) { // we create a new array for each top level categories $categories[$id] = array('name' => $name, 'children' => array()); } else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); } } // build combo box options $list = ''; foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; $list .= "<optgroup label=\"$name\">"; foreach ($children as $child) { $list .= "<option value=\"{$child['id']}\""; if ($child['id'] == $catId) { $list.= " selected"; } $list .= ">{$child['name']}</option>\r\n"; } $list .= "</optgroup>"; } return $list; }
  10. Firstly appologies I think I posted my first post (this one) in the wrong bit In terms of the database structure the table for categories has id and parent id fields so this as my understanding should mean that you can assign any number of parent / child relationships. Below is the code from the actual page that calls the function: <?php if (!defined('WEB_ROOT')) { exit; } if (isset($_GET['catId']) && (int)$_GET['catId'] > 0) { $catId = (int)$_GET['catId']; $sql2 = " AND p.cat_id = $catId"; $queryString = "catId=$catId"; } else { $catId = 0; $sql2 = ''; $queryString = ''; } // for paging // how many rows to show per page $rowsPerPage = 20; $sql = "SELECT pd_id, c.cat_id, cat_name, pd_name, pd_thumbnail FROM tbl_product p, tbl_category c WHERE p.cat_id = c.cat_id $sql2 ORDER BY pd_name"; $result = dbQuery(getPagingQuery($sql, $rowsPerPage)); $pagingLink = getPagingLink($sql, $rowsPerPage, $queryString); $categoryList = buildCategoryOptions($catId); ?> <p> </p> <form action="processProduct.php?action=addProduct" method="post" name="frmListProduct" id="frmListProduct"> <table width="100%" border="0" cellspacing="0" cellpadding="2" class="text"> <tr> <td align="right">View products in : <select name="cboCategory" class="box" id="cboCategory" onChange="viewProduct();"> <option selected>All Category</option> <?php echo $categoryList; ?> </select> </td> </tr> </table> <br> <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="text"> <tr align="center" id="listTableHeader"> <td>Product Name</td> <td width="75">Thumbnail</td> <td width="75">Category</td> <td width="70">Modify</td> <td width="70">Delete</td> </tr> <?php $parentId = 0; if (dbNumRows($result) > 0) { $i = 0; while($row = dbFetchAssoc($result)) { extract($row); if ($pd_thumbnail) { $pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail ."_64.png"; } else { $pd_thumbnail = WEB_ROOT . 'images/no-image-small.png'; } if ($i%2) { $class = 'row1'; } else { $class = 'row2'; } $i += 1; ?> <tr class="<?php echo $class; ?>"> <td><a href="index.php?view=detail&productId=<?php echo $pd_id; ?>"><?php echo $pd_name; ?></a></td> <td width="75" align="center"><img src="<?php echo $pd_thumbnail; ?>"></td> <td width="75" align="center"><a href="?c=<?php echo $cat_id; ?>"><?php echo $cat_name; ?></a></td> <td width="70" align="center"><a href="javascript:modifyProduct(<?php echo $pd_id; ?>);">Modify</a></td> <td width="70" align="center"><a href="javascript:deleteProduct(<?php echo $pd_id; ?>, <?php echo $catId; ?>);">Delete</a></td> </tr> <?php } // end while ?> <tr> <td colspan="5" align="center"> <?php echo $pagingLink; ?></td> </tr> <?php } else { ?> <tr> <td colspan="5" align="center">No Products Yet</td> </tr> <?php } ?> <tr> <td colspan="5"> </td> </tr> <tr> <td colspan="5" align="right"><input name="btnAddProduct" type="button" id="btnAddProduct" value="Add Product" class="box" onClick="addProduct(<?php echo $catId; ?>)"></td> </tr> </table> <p> </p> </form>
  11. Hello everyone, I am currently working on the script taken from www.phpwebcommerce.com I am only a beginner and have been using it in a little project for an online game just to help my mates be able to trade items and such. I have managed to get almost everything sorted apart from one thing. The function which gets the categories for a drop down select box: /* Generate combo box options containing the categories we have. if $catId is set then that category is selected */ function buildCategoryOptions($catId = 0) { $sql = "SELECT cat_id, cat_parent_id, cat_name FROM tbl_category ORDER BY cat_id"; $result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error()); $categories = array(); while($row = dbFetchArray($result)) { list($id, $parentId, $name) = $row; if ($parentId == 0) { // we create a new array for each top level categories $categories[$id] = array('name' => $name, 'children' => array()); } else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); } } // build combo box options $list = ''; foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; $list .= "<optgroup label=\"$name\">"; foreach ($children as $child) { $list .= "<option value=\"{$child['id']}\""; if ($child['id'] == $catId) { $list.= " selected"; } $list .= ">{$child['name']}</option>\r\n"; } $list .= "</optgroup>"; } return $list; } Basically the problem is that it only creates a category list of two levels e.g. Parent Child and I wish to be able to have more sub categories e.g. Parent Child Child Child Is there anyway of adapting this code so it can do something like that? Many thanks for any help.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.