krio Posted August 27, 2009 Share Posted August 27, 2009 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] ++; } Link to comment https://forums.phpfreaks.com/topic/172174-solved-increment-specific-keys-value-based-on-get-variable/ Share on other sites More sharing options...
mikesta707 Posted August 27, 2009 Share Posted August 27, 2009 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 Link to comment https://forums.phpfreaks.com/topic/172174-solved-increment-specific-keys-value-based-on-get-variable/#findComment-907815 Share on other sites More sharing options...
krio Posted August 27, 2009 Author Share Posted August 27, 2009 thanks for the tip! i've got it pretty close to where i want it.. just one more thing... $_SESSION ['quantity'] [$key] ++; this is making the actually key increment. How can i make the value increment? thanks Link to comment https://forums.phpfreaks.com/topic/172174-solved-increment-specific-keys-value-based-on-get-variable/#findComment-907822 Share on other sites More sharing options...
mikesta707 Posted August 27, 2009 Share Posted August 27, 2009 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 Link to comment https://forums.phpfreaks.com/topic/172174-solved-increment-specific-keys-value-based-on-get-variable/#findComment-907824 Share on other sites More sharing options...
krio Posted August 27, 2009 Author Share Posted August 27, 2009 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 Link to comment https://forums.phpfreaks.com/topic/172174-solved-increment-specific-keys-value-based-on-get-variable/#findComment-907843 Share on other sites More sharing options...
krio Posted August 27, 2009 Author Share Posted August 27, 2009 it seems like that problem was being caused by the foreach because once i switch to for() everything is working.. THanks!! Link to comment https://forums.phpfreaks.com/topic/172174-solved-increment-specific-keys-value-based-on-get-variable/#findComment-907853 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.