Jump to content

session


raptor30506090

Recommended Posts

Hi guys how would i get this in to a session

 

 

<?php
$products = array(array(
				  'Code' => 'TIR',
				  'Description' => 'Tires',
				  'Price' => 10,
				  'Name' => 'Darren' 
			        ), 
			 );

                                 for($row = 0; $row < 1; $row++){
									  while(list($key, $value) = each($products[$row])){

										  echo $value;
										 }
									 }
?>

Link to comment
Share on other sites

Guy im still stuck what im trying to do is

1: create a shopping cart option and session['cart']

 

 

2: options

like a shed cost ?500 size 10x10

then adds an option to have the front made in bars

and then also orders the same with out the option of bars it has the same id so in my session it over rights can any one tell me the right way to go about it or spell it out to me.

 

Cheers

Link to comment
Share on other sites

So i now im a pain

 

I had a look at what you said and still couldnt work it out as you can tell im new at carts

 

what im trying to do is in the products have options with price changes but when i do this it over rights the last one in the session with same id

 

so if you order same type of shed say 10x10 at $500 then they what same type again but 10x20 at $750 i would like a new product in the cart but i keep over righting last session

 

Many thanks Daz

Link to comment
Share on other sites

Like this.  This is exactly what I suggested, it was 20 lines counting comments:

 

 

<?php


function addToCart ( $productId, $quantity, $size, $color, $price ) {
  //if the productID doesn't exist, just create it:
  if ( !isset($_SESSION['cart'][$productId]) ) {
    $_SESSION['cart'][$productId] = array(array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price));
    return;
  } 
  
  //product already exists, check to see if it's the same as another in the group:
  foreach ( $_SESSION['cart'][$productId] as &$product ) {
    //NOTE:  If you want to override pricing with new tiers, you can remove 'price' from this comparison.  You may also want to 
    //       fetch prices from the database when you DISPLAY the cart if you do bulk discounts
    if ( $product['price'] == $price && $product['color'] == $color && $product['size'] == $size ) {
      $product['quantity'] += $quantity;
      return;
    }
  }
  
  //if we've reached here, the productID is already in the cart but the current item is not a duplicate of an existing one:
  $_SESSION['cart'][$productId][] = array('quantity' => $quantity, 'size' => $size, 'color' => $color, 'price' => $price);
}



//TEST CODE

//set up the cart:
$_SESSION['cart'] = array(
  '123' => array(array('quantity' => 2, 'size' => '10x10', 'color' => 'blue', 'price' => '899.95')),
  '456' => array(array('quantity' => 1, 'size' => 'Large', 'color' => 'green', 'price' => '9.95'), array('quantity' => 3, 'size' => 'Small', 'color' => 'black', 'price' => '8.99')),
  );
  
  
echo "<pre>\n\n";
print_r($_SESSION['cart']);echo "\n\n";
  
  
//add a new green large 456:
addToCart('456', 1, 'Large', 'green', 9.95);
print_r($_SESSION['cart']);echo "\n\n";

//add three different sized 123 products:
addToCart('123', 3, '5x5', 'blue', 499.95);
print_r($_SESSION['cart']);echo "\n\n";


//add a completely new product offering:
addToCart('789', 30, 'Refill', 'yellow', .90);
print_r($_SESSION['cart']);echo "\n\n";

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.