Jump to content

Adding Quanity to My Script


TheJoey

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/174472-adding-quanity-to-my-script/
Share on other sites

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" />

 

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 />";
}

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) 
		   );

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.