Freedom-n-Democrazy Posted October 8, 2011 Share Posted October 8, 2011 I have a problem where the session field quantity is not updating when the same entity key (id) is added to the session. Instead of quantity being updated, another identical entity is being added to the session. I am TOTALLY out of clues on this one! Does anyone have any idea what is causing this to happen? <?php session_start(); if ($_SESSION['cart']['content']['id'] == $_POST['id']) { $_SESSION['cart']['content']['$_POST[id]']['quantity'] = $_SESSION['cart']['content']['$_POST[id]']['quantity'] + $_POST['quantity']; } else { $_SESSION['cart']['content'][] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity']); } echo '<DIV class="result">Added to cart.</DIV>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/ Share on other sites More sharing options...
awjudd Posted October 8, 2011 Share Posted October 8, 2011 http://php.net/string More specifically this piece right here: '$_POST[id]'. That will always return the same value. if you echo it you will see that it is saying $_POST[id] exactly how you have it. <?php session_start(); if ($_SESSION['cart']['content']['id'] == $_POST['id']) { $_SESSION['cart']['content'][$_POST['id']]['quantity'] = $_SESSION['cart']['content'][$_POST['id']]['quantity'] + $_POST['quantity']; } else { /* You were just appending it to the array but you are doing a search for it above so you need the actual id as the index */ $_SESSION['cart']['content'][$_POST['id']] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity']); } echo '<DIV class="result">Added to cart.</DIV>'; ?> ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277229 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 8, 2011 Author Share Posted October 8, 2011 echo $_SESSION['cart']['content'][$_POST['id']]['quantity']; and echo $_SESSION['cart']['content'][$_POST[id]]['quantity']; ... are both giving me the same result, which are both correct. Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277233 Share on other sites More sharing options...
awjudd Posted October 8, 2011 Share Posted October 8, 2011 $_POST[id] will give you a warning (if you have error reporting displaying them) because it has to automagically convert id to 'id'. If you have a $_POST'd value that has a space or anything in it (i.e. $_POST [ 'foo bar' ]) then it won't work. You should always use the quotes rather than not because it is incredibly slower (about 4 times). ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277245 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 8, 2011 Author Share Posted October 8, 2011 Your right. I remember seeing PHP assume in my error log file last week over the same thing. Thanks dude. Do you know why quantity is not updating in the code? Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277246 Share on other sites More sharing options...
awjudd Posted October 8, 2011 Share Posted October 8, 2011 What is it doing instead? ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277248 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 8, 2011 Author Share Posted October 8, 2011 Its adding another identitcal entity when It should updaing the quantity to 2: http://i56.tinypic.com/s44ef9.png I cannot understand why...... not 1 bit! Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277250 Share on other sites More sharing options...
awjudd Posted October 8, 2011 Share Posted October 8, 2011 if ($_SESSION['cart']['content']['id'] == $_POST['id']) { Should be: if ( isset ( $_SESSION [ 'cart' ] [ 'content' ] [ $_POST [ 'id' ] ) ) { ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277267 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 9, 2011 Author Share Posted October 9, 2011 You mean: if ( isset ( $_SESSION [ 'cart' ] [ 'content' ] [ $_POST [ 'id' ] ) ) { Thanks allot dude! Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277321 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 9, 2011 Author Share Posted October 9, 2011 Unfortunately, I spoke too soon. Now another problem has developed. If I add an entity with a quantity of 1, and then add the same entity with a quantity of 1, I get a single entity with a quantity of 2, however, If I add an entity with a quantity of 1, and then add the same entity with a quantity of 3, I get two entities - entity A/quantity-1 & entity A/quantity-3. Why is this happening? http://i52.tinypic.com/30bgtww.png <?php session_start(); if (isset($_SESSION['cart']['content'][$_POST['id']])) { $_SESSION['cart']['content'][$_POST[id]]['quantity'] += $_POST['quantity']; } else { $_SESSION['cart']['content'][] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity']); } echo '<DIV class="result">Added to cart.</DIV>'; echo $_SESSION['cart']['content'][$_POST['id']]['quantity']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277329 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 9, 2011 Author Share Posted October 9, 2011 Never mind. I have re-wrote the way entities are stored in the session. They are now stored as: $_SESSION['cart']['content'][$_POST[id]][$_POST[size]] = array ('quantity' => $_POST['quantity']) opposed to: $_SESSION['cart']['content'][] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity']) EDIT: Well, this is flawed. But I will figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277338 Share on other sites More sharing options...
awjudd Posted October 9, 2011 Share Posted October 9, 2011 Because you still just appending to the end of the array when you should be doing as I said and: $_SESSION['cart']['content'][$_POST['id']] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity']); ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248693-_session-field-will-not-update/#findComment-1277422 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.