Jump to content

[SOLVED] increment specific key's value based on GET variable


Recommended Posts

hello,

 

if a value exists in $_SESSION['id'] i want to get the key and find that key in $_SESSION['quantity'] and increment the value by 1.

 

am i on the right track?

 

Thanks

 

session_start ();

$_SESSION ['id'] = array (5);
$_SESSION ['quantity'] = array (3);

$prodToAdd = $_GET ['id'];


if (in_array ( $prodToAdd, $_SESSION ['id'] )) {

$key = array_search ( $prodToAdd, $_SESSION ['id'] );
$_SESSION ['quantity'] [$key] ++;

} 

well, in this script whatever value your session['id'] had was erased when you instantiated it as a new array. so I don't think that the if statement will ever run true. Where exactly is your session['id'] getting its values from? if its getting them from another page, and you are sure that the values are there (and being added correctly), than there is no need to instantiate it as a new array, as it is already an array

hmm.. that shouldnt be making the key increment. It should make the value of that specific entry in the array increment. try deleting the space between your square brackets. I don't know why that would be causing an error, but i've never actually seen anyone put spaces between everything like that so it might be confusing things

hey thanks again for helping me out

 

It seems like the ++ is not only incrementing the value like it is supposed to, but it is also creating a new key with the same value

 

so my output is looking like this once I refresh the page a few times:

 

0 7

1 3

2 3

3 3

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.