halojoy wrote:
I have added $match = false;
When an item is found and 'qty' updated, we set
$match=true;
and break out from search loop with
break;
So you've added a break and changed the name of the flag variable.
Are the names that are used to identify items unique? If they are, can they be used as identifiers in the array instead of an (arbitrary) key?
if(isset($_GET['additem'])
{
if(isset($_SESSION['cart'][$_GET['additem']]))
{
$_SESSION['cart'][$_GET['additem']]['qty']++;
echo "Item Updated: ".$_GET['additem']."<br/><br/>";
}
else
{
$_SESSION['cart'][$_GET['additem']] = array('qty'=>1);
echo "Item Added: ".$_GET['additem']."<br/><br/>";
}
}
If qty is the only thing being stored, then it doesn't need to be stored as a one-element array, but directly:
if(isset($_GET['additem'])
{
if(isset($_SESSION['cart'][$_GET['additem']]))
{
$_SESSION['cart'][$_GET['additem']]++;
echo "Item Updated: ".$_GET['additem']."<br/><br/>";
}
else
{
$_SESSION['cart'][$_GET['additem']] = 1;
echo "Item Added: ".$_GET['additem']."<br/><br/>";
}
}
Note throughout this that there is nothing to check that $_GET['additem'] is legitimate.