Jump to content

arrays and sessions


raptor30506090

Recommended Posts

Hi guy could any the point me in the right direction:

 

Im trying to build my first shopping cart but stuck at the start i now im going to use sessions this is what i have.

 

if (!isset($_session['cart'])) $_SESSION["cart"]= '$id' ;
if (isset($_POST['id']) &&!empty($_POST['id']) && isset($_POST['cost']) &&!empty($_POST['cost']))  {


$id = $_POST['id'];
$cost = $_POST['cost'];

$item = array($id => $cost);

$_SESSION['cart']=$item;


foreach($_SESSION['cart'] as $key => $cost){
                                      echo $key . ' '. $cost;
 }
} 

echo '<pre>'; 
print_r($_SESSION); 
echo '</pre>';						 

 

the question is i have the id and the  cost  from the form but in the cart array how do i add to the cost each time button pressed

 

Many thanks 

Link to comment
Share on other sites

not 100% sure what you mean

 

are you looking to have an add qty button to add multiple items, perhaps ajax?

 

or a refresh each time they click add item: in which case i would structure your session array alittle different and add qty to the fields ie:

 

  if (!isset($_session['cart'])) $_SESSION["cart"]= '$id' ;

  if (isset($_POST['id']) &&!empty($_POST['id']) && isset($_POST['cost']) &&!empty($_POST['cost']))  {

$id = $_POST['id'];
$cost = $_POST['cost'];

if($_SESSION['cart'][$id][qty]>0)
{
  $_SESSION['cart'][$id][qty]++;
}
else
{
  $_SESSION['cart'][$id][cost]=$cost);
  $_SESSION['cart'][$id][qty]=1;
}


foreach($_SESSION['cart'] as $key => $arr){
                                      echo $key . ' '. $arr[cost].' '.$arr[qty].' TOTAL:$pound;'.number_format($arr[qty]*$arr[cost],2));
 }
} 

echo '<pre>'; 
print_r($_SESSION); 
echo '</pre>';						 

Link to comment
Share on other sites

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.