TheJoey Posted September 16, 2009 Share Posted September 16, 2009 Hi im having the problem trying to implement quanity into my shopping cart that uses sessions and arrays <?php session_start(); if(!isset($_SESSION['shop'])) { $_SESSION['shop'] = array(); } // Adding to Cart: if(isset($_GET['action']) && $_GET['action'] == "add_to_cart") { $_SESSION['shop'][] = $_GET['item']; } // Removing From Cart: if(isset($_GET['action']) && $_GET['action'] == "remove_item") { unset($_SESSION['shop'][$_GET['itemkey']]); } ?> <?php echo "<b>Shopping Cart</b><br />"; foreach($_SESSION['shop'] as $key => $value) { echo $value . " <a href='" . $_SERVER['PHP_SELF'] . "?action=remove_item&itemkey=" . $key ."'>Remove</a><br />"; } ?> <a href='<?PHP echo $_SERVER['PHP_SELF']; ?>?action=add_to_cart&item=itemprodid1'>item1/a><br /> <a href='<?PHP echo $_SERVER['PHP_SELF']; ?>?action=add_to_cart&item=itemprodid2'>item2</a><br /> <tr><td>Qty:</td><td><input type="text" name="qty"> the only problem is i dont know how i would add this so that i can latter retrieve it for use for my cart latter Quote Link to comment https://forums.phpfreaks.com/topic/174472-adding-quanity-to-my-script/ Share on other sites More sharing options...
TheJoey Posted September 17, 2009 Author Share Posted September 17, 2009 i was thinking something like <input name="pid" type="hidden" id="pid" value="<?=$_REQUEST['item1']?>" /> <input name="add_to_cart" type="submit" alt="Add to Cart" class="" id="add_to_basket" value="Add to Cart" /> <input name="pid" type="hidden" id="pid" value="<?=$_REQUEST['item2']?>" /> <input name="add_to_cart" type="submit" alt="Add to Cart" class="" id="add_to_basket" value="Add to Cart" /> Quote Link to comment https://forums.phpfreaks.com/topic/174472-adding-quanity-to-my-script/#findComment-919866 Share on other sites More sharing options...
TheJoey Posted September 17, 2009 Author Share Posted September 17, 2009 Is there another way i can go about doing a databaseless shopping cart? Quote Link to comment https://forums.phpfreaks.com/topic/174472-adding-quanity-to-my-script/#findComment-919947 Share on other sites More sharing options...
TheJoey Posted September 17, 2009 Author Share Posted September 17, 2009 Is there another way i can go about doing a databaseless shopping cart? Quote Link to comment https://forums.phpfreaks.com/topic/174472-adding-quanity-to-my-script/#findComment-920110 Share on other sites More sharing options...
KevinM1 Posted September 17, 2009 Share Posted September 17, 2009 Is there another way i can go about doing a databaseless shopping cart? Each item has a name, a price, and a quantity, correct? Store that info in an associative array: $item["name"] = $name; $item["price"] = $price; $item["quantity"] = $quantity; $_SESSION["cart"][] = $item //accessing the data.... foreach($_SESSION["cart"] as $item) { echo "Item: {$item["name"]} <br />"; echo "Individual price: {$item["price"]} <br />"; echo "Quantity purchased: {$item["quantity"]} <br />"; echo "Total for {$item["name"]}: " . $item["price"] * $item["quantity"] . "<br /><br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/174472-adding-quanity-to-my-script/#findComment-920142 Share on other sites More sharing options...
TheJoey Posted September 17, 2009 Author Share Posted September 17, 2009 how is it possible to store so much information in a single array? Quote Link to comment https://forums.phpfreaks.com/topic/174472-adding-quanity-to-my-script/#findComment-920156 Share on other sites More sharing options...
KevinM1 Posted September 17, 2009 Share Posted September 17, 2009 how is it possible to store so much information in a single array? Why wouldn't it be? Jagged arrays (a.k.a., an array of arrays) are pretty common in programming. Quote Link to comment https://forums.phpfreaks.com/topic/174472-adding-quanity-to-my-script/#findComment-920178 Share on other sites More sharing options...
TheJoey Posted September 18, 2009 Author Share Posted September 18, 2009 could i do it this way? $_SESSION["cart"] = array( array("item1", ), array("price", 0.75 ), array("quantity", 2) ); array( array("item2", ), array("price", 1.75 ), array("quantity", 3) ); Quote Link to comment https://forums.phpfreaks.com/topic/174472-adding-quanity-to-my-script/#findComment-920488 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.