I'm in the process of learning PHP and I'm currently trying to get to grips with multidimensional arrays.
Basically I've got a two dimensional array with each row having a total of 3 columns which are product ; price and quantity. These arrays will need to be stored in a session as they will eventually form part of a shopping cart.
For example:
$HTTP_SESSION_VARS['cart'] = array(array('Bag of Nails','2.00','10'));
I now have an array called cart with one row and three columns.
What I can't work out is:
1. How to dynamically add another row to this array
2. How to dynamically find the product attribute and change the quantity.
I found this very easy in coldfusion, just haven't got to grips with accessing arrrays in PHP. can someone help me out
Richard
Array - 2 dimensional ones
Moderator: General Moderators
$HTTP_SESSION_VARS['cart'] = array(array('Bag of Nails','2.00','10'));1. How to dynamically add another row to this array
you could get this by putting in a loop...
$HTTP_SESSION_VARS['cart'][0] = array('Bag of Nails','2.00','10');
$HTTP_SESSION_VARS['cart'][1] = array('Bag of hammers','4.00','20');
$HTTP_SESSION_VARS['cart'][2] = array('Bag of glue','6.00','30');
or
$HTTP_SESSION_VARS['cart'] = array(
array('Bag of Nails','2.00','10'),
array('Bag of hammers','4.00','20'),
array('Bag of glue','6.00','30'));
both are the same...
2. How to dynamically find the product attribute and change the quantity.
$HTTP_SESSION_VARS['cart'][0][0] == 'Bag of Nails';
$HTTP_SESSION_VARS['cart'][0][1] == '2.00';
$HTTP_SESSION_VARS['cart'][0][2] == '10';
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
should be$HTTP_SESSION_VARS['cart'][0] = array('Bag of Nails','2.00','10');
$HTTP_SESSION_VARS['cart'][1] = array('Bag of hammers','4.00','20');
$HTTP_SESSION_VARS['cart'][2] = array('Bag of glue','6.00','30');
Code: Select all
<?php
while (1) { //whatever your loop may be
$_SESSION['cart'][] = array('name'=>"$tool", 'price'=>"$price",'someothervar'=>"$somevar");
}
?>- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
A little confused now...so many replies
$HTTP_SESSION_VARS['cart'][0] = array('Bag of Nails','2.00','10');
$HTTP_SESSION_VARS['cart'][1] = array('Bag of hammers','4.00','20');
$HTTP_SESSION_VARS['cart'][2] = array('Bag of glue','6.00','30');
If I wanted to add another row would i simply increment cart by 1 and if so could you possible give me a clue how to do it.
$HTTP_SESSION_VARS['cart'][1] = array('Bag of hammers','4.00','20');
$HTTP_SESSION_VARS['cart'][2] = array('Bag of glue','6.00','30');
If I wanted to add another row would i simply increment cart by 1 and if so could you possible give me a clue how to do it.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Unless you are using PHP < 4.1 then use $_SESSION instead of $HTTP_SESSION_VARS.
Also can you all ('cept Phenom who does it anyway) remember to use the tags around your PHP code.
Mac
Also can you all ('cept Phenom who does it anyway) remember to use the
Code: Select all
Mac