Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding multiple elements to an array

Status
Not open for further replies.

dessie1981

Programmer
May 9, 2006
116
GB
Hi Everyone,

I'm currently designing an online store and i am having trouble adding multiple items to the shopping cart, i.e. 2 or 3 or whatever amount of the same item to the cart.

The shopping cart is simply an array stored in a session
Anyone got any good ideas on how this can be done or know any functions in php that can help me.

Was thinking of using an associative array and letting the keys be equal to the quantity.
 
i'd usually do it something like this

Code:
$cart = array (
         $itemid=>array(
                  "quantity"=>$quantity,
                  "pricequoted"=$price
                       )
               );

ie use the itemid as the key and the quantity and otherdata as the values to that key.
 
Hi ,

I could not get that bit of code above to work but i tried something simialr and found that the quantity is being used as the array position, is there any way to have two elements in the same position, one as the productid and the other as the quantity of that product.

here is what i did earlier

<?
$product1 = "PC";
$product2 = "Monitor";

$quantity1 = 20;
$quantity2 = 12;


$cart = array($quantity1=>$product1, $quantity2=>$product2);

?>
 
on my previous post i had omitted a greater than sign. it should have been
Code:
$cart = array (
         $itemid=>array(
                  "quantity"=>$quantity,
                  "pricequoted"=?$price
                       )
               );

$cart = array($quantity1=>$product1, $quantity2=>$product2);

this is the wrong way around. the keys of an array must be unique and there is a good chance that the quantity of each item may not be unique.

however as productids are nearly always unique you can reverse the logic
Code:
$cart = array($product1=>quantity1, $product2->$quantity2);

this is semantically equivalent to
Code:
$cart[$product1] = $quantity1;
$cart[$product2] = $quantity2;
which is often an easier way of programmatically building arrays.
 
Ok I get you but i need to be able to retrieve the elements from the array by stepping though it,

$array[0] 1st element in cart
$array[1] 2nd element in cart
etc.....

What would be ideal would be

$array[0] = <productid of 1st element>, <quantity of first element>
and so on....................

What do you think?
Maybe using two seperate arrays in the session could be the solution.
 
Found the solution by using 2 arrays in the session one for the productid's and one for the quantities.

Thanks
Dessie
 
you can still you the same method
Code:
$cart[] = array($product1=>quantity1, $product2->$quantity2);
or
Code:
$cart[][$product1] = $quantity1;
$cart[][$product2] = $quantity2;

but you can also step through the array in my previous post

Code:
foreach ($cart as $productid=>$quantity):
//do stuff
endforeach;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top