Forum Moderators: coopster
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?
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?
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