1

i've looked and looked, and can't find out how to change a session variable and store it back into the array. my code does what i want so far, and adds 1 to the variable, but i don't know how to save it back into the array. my code is as follows.

if(isset($_GET['action']) && $_GET['action'] == 'addp')
{
echo "trying to add 1 item to serial ".$_GET['id']."<br>";
$product_code = filter_var($_GET['id'], FILTER_SANITIZE_STRING);
if(isset($_SESSION['products']))
{
    $number = 0;
    foreach($_SESSION['products'] as $cart_itm)
    {

        if($cart_itm['code'] == $product_code)
        {               
            $a = array($_SESSION['products']);
            foreach($_SESSION['products'] as $a){
                foreach($a as $b){
                    while(list($key, $val) = each($a)){
                        if($key == 'qty'){
                            $val = $val + 1;
                            echo $val;
                        }
                    }
                }
            }               

        }
        else
        {
            echo"Item Code Did not Match";
        }
        $number++;
    }
}
else
{
    echo"Session['Products'] Not Set";
}
}
else
{
    echo"Action is set to ".$_GET['action'];
}

Any help, even if its pointing me at a post I failed to look at would help.

Also, any pointers on code style would be appreciated.

5
  • A print_r of the $_SESSION array and what you want it changed to and the criteria to change it would help. Commented Dec 4, 2014 at 0:00
  • Why 2 foreachs of $_SESSION['products']? Isn't $a the same as $cart_itm? Commented Dec 4, 2014 at 0:02
  • @abracadaver so to change it it'd be print_r($cart_itm[$number]['qty']) ? Commented Dec 4, 2014 at 0:15
  • @helmet648 What he meant was: check your $_SESSION['products'] and show us the structure of it Commented Dec 4, 2014 at 0:17
  • Ahhhh, k, thanks! that helped me a bit. i got the code working like i want to. Commented Dec 4, 2014 at 0:31

3 Answers 3

1

This should probably do:

if($key == 'qty'){
    $_SESSION['products'][$key] = $val + 1; //this line
    $val = $val + 1;
    echo $val;
}

If not, use the same concept: you have the key which allow you to specify which item in the array you want to change.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks man! this along with the comments to my question helped me set the variable
0

Try this:

if(isset($_SESSION['products']))
{
    $number = 0;
    foreach($_SESSION['products'] as $cart_itm)
    {
        if($cart_itm['code'] == $product_code)
        {               
            $_SESSION['products']['qty']++; //here is the quantity of the item, no need of more loops
            echo $_SESSION['products']['qty'];
        }
        else
        {
            echo"Item Code Did not Match";
        }
        $number++;
    }
}

Comments

0

what i did to fix the situation

if(isset($_SESSION['products']))
{

$number = 0;

foreach($_SESSION['products'] as $cart_itm)
{
    if($cart_itm['code'] == $product_code)
    {               

        $_SESSION['products'][$number]['qty'] = $cart_itm['qty'] + 1;
    }
    else
    {
        echo"Item Code Did not Match";
    }
    $number++;
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.