Forum Moderators: coopster

Message Too Old, No Replies

Modify array item for session based cart

         

glyphomat

11:40 pm on Jan 7, 2005 (gmt 0)

10+ Year Member



Hi - I'm building a shopping cart and need to add the function so if an item is selected twice, the 'quantity' counter increments

I can't get the syntax right for modifying the array value.

I have this:

foreach ($_SESSION['basket'] as $key => $row)
{
if ( $row['gnsz']==$gnsz)
{
$newqty=$row['qty'] ;
$newqty++;
$row['qty']= $newqty;
}
}

it picks up the first qty value but I can't seem to modify it in the array

I'm making a dumb mistake somewhere

thanks

-- does anybody else find that as soon as you post the question you suddenly figure out what was wrong?

jatar_k

12:35 am on Jan 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



does that mean you figured it out?

I am seeing some confusion in your foreach usage but I can't be sure as I don't know your $_SESSION structure.

Can you tell me what an explicit var would be?

can you rewrite this just with one session var

if ($row['gnsz']==$gnsz) {
$newqty=$row['qty'] ;
$newqty++;
$row['qty']= $newqty;
}

how would $row['gnsz'] actually be accessed in the $_SESSION?
is it $_SESSION['basket']['gnsz'] or something else?

glyphomat

2:37 pm on Jan 8, 2005 (gmt 0)

10+ Year Member



Hi Jatar - no I didn't figure this one out - (but I did figure out another problem that was going to be another part of the post!)

the array is multidimensional, built like this:

- these are got from the url as usual:
$gnsz=$_GET['gnsz'];
$pr=$_GET['pr'];
$sz=$_GET['sz'];
$stkid=$_GET['stkid'];

then - with some code left out:

if (!isset($_SESSION['basket'])){
//create new basket
$_SESSION['basket']=array();
}

//check if item exists and increment qty if so
foreach ($_SESSION['basket'] as $key => $row)
{
if ( $row['gnsz']==$gnsz)
{
$newqty=$row['qty'] ;
$newqty++;
$row['qty']= $newqty;
$increment=TRUE;
}
}

// if new item add new row to array
if (!$increment)
{ $qty=1;
$_SESSION['basket'][]= array ('gnsz' => $gnsz, 'qty' => $qty, 'pr' => $pr, 'sz' => $sz, 'stkid' => $stkid);
}
// end of add item stuff

.. then to poulate a table and pull out values:

foreach ($_SESSION['basket'] as $key => $row)
{
$gnsz=$row['gnsz'];
etcetera...
}
the key being 0,1,2 for each item added

(gnsz is my all purpose id)

I dont understand what you mean about an explicit var?
Do I know what one is? Yes - but I couldn't be sure about what an "implicit" var would be...

Any help much appreciated, I'm new to sessions and multidim arrays

glyphomat

3:08 pm on Jan 8, 2005 (gmt 0)

10+ Year Member



ho hum, well I did figure it out:

$_SESSION['basket'][$key] ['qty']++;

now I need to find how to mark it resolved..

jatar_k

6:56 pm on Jan 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you just did by saying you got it figured ;)