Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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