Logician Posted November 3, 2011 Share Posted November 3, 2011 Is it possible to add onto the value of a $_SESSION, rather replacing it? $_SESSION['order']['cartcontenttext'] += echo 'Small: "'.$content['sizes'].'"'; $_SESSION['order']['cartcontenttext'] += echo 'Medium: "'.$content['sizem'].'"'; $_SESSION['order']['cartcontenttext'] += echo 'Large: "'.$content['sizel'].'"'; ... so the result would be something like this: echo $_SESSION['order']['cartcontenttext']; Small: 1Medium: 3Large: 2 Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/ Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 Yes, but there is no need for echo or all the unnecessary quoting. $_SESSION['order']['cartcontenttext'] .= echo 'Small: ' . $content['sizes']; Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284585 Share on other sites More sharing options...
phporcaffeine Posted November 3, 2011 Share Posted November 3, 2011 $_SESSION['order']['cartcontenttext'] = "Some additional text at the beginning " . $_SESSION['order']['cartcontenttext']; $_SESSION['order']['cartcontenttext'] = $_SESSION['order']['cartcontenttext'] . " Some additional text at the end"; $_SESSION['order']['cartcontenttext'] = 'Small: ' . $content['sizes'] . ' Medium: ' . $content['sizem'] . ' Large: ' . $content['sizel']; (You could add line breaks ("/n") or HTML break tags ("<br />"), if you didn't want it in a single line) Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284590 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 Thorpe, I tried your suggestion, but it didn't work and I didn't get an error message in my log: foreach ($_SESSION['cart']['content'] as $content) { $query = "select * from products where id='{$content['id']}'"; $result = mysql_query($query); $row = mysql_fetch_array($result); if ($content['sizes'] == 0) {goto sizem;} else {$_SESSION['order']['cartcontenttext'] .= echo 'Small: '.$content['sizes'];} sizem: if ($content['sizem'] == 0) {goto sizel;} else {$_SESSION['order']['cartcontenttext'] .= echo 'Medium: '.$content['sizem'];} sizel: if ($content['sizel'] == 0) {goto sizexl;} else {$_SESSION['order']['cartcontenttext'] .= echo 'Large: '.$content['sizel'];} sizexl: if ($content['sizexl'] == 0) {goto endsizes;} else {$_SESSION['order']['cartcontenttext'] .= echo 'Extra large: '.$content['sizexl'];} endsizes: } I'll try your suggestion, phpORcaffine. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284599 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 Obviously you missed the part about not needing echo. And please, drop the goto's! Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284603 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 Ok, so I tried this. It looks like exactly what I need, but I suspect I have mucked up the braking out? $_SESSION['order']['cartcontenttext'] = '.if ($content['sizes'] == 0) {goto sizem;} else {echo 'Small: '.$content['sizes']; sizem: if ($content['sizem'] == 0) {goto sizel;} else echo 'Medium: '.$content['sizem']; sizel: if ($content['sizel'] == 0) {goto sizexl;} else {echo 'Large: '.$content['sizel']; sizexl: if ($content['sizexl'] == 0) {goto endsizes;} else {echo 'Extra large: '.$content['sizexl']; endsizes:} Thorpe, what is wrong with goto's? Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284612 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 This is the code being used in whole: foreach ($_SESSION['cart']['content'] as $content) { $query = "select * from products where id='{$content['id']}'"; $result = mysql_query($query); $row = mysql_fetch_array($result); $_SESSION['order']['cartcontenttext'] = if ($content['sizes'] == 0) {goto sizem;} else {echo 'Small: '.$content['sizes'];} sizem: if ($content['sizem'] == 0) {goto sizel;} else {echo 'Medium: '.$content['sizem'];} sizel: if ($content['sizel'] == 0) {goto sizexl;} else {echo 'Large: '.$content['sizel'];} sizexl: if ($content['sizexl'] == 0) {goto endsizes;} else {echo 'Extra large: '.$content['sizexl'];} endsizes:; $query = "INSERT INTO orders (id, cartcontent) VALUES (null, '".$_SESSION['order']['cartcontenttext']."'"; mysql_query ($query); } Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284623 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 You seem to missing the absolute basics. If statements do not return values, just like foreach statements do not return any value. I think you should get yourself a book and read it from start to end. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284626 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 LOL Well, do you have any suggestion as a way to achieve what I am trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284630 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 Not really. At this point, you really need to learn some basic php and there are plenty of books around that will teach you that. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284632 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 28,097 posts and nothing to suggest, I'd have to recommend the same. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284634 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 I have given you what I consider to be the most appropriate suggestion. It's not my fault if you won't listen too the advice. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284635 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 Yes. Fair enough. I am now going to take 2nd best. I've mind hammered myself so much I am temporarily uncapable of detecting minor erros. Could you please help me with this, its not working can I cannot see why right now: $_SESSION['order']['cartcontenttext'] = 'Small: '.$content['sizes'].'Medium: '.$content.'Large: '.$content['sizel'].'Extra large: '.$content['sizexl']'; Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284636 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 You have an extra quote on the end, otherwise: Define 'not working'. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284637 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 Defined by PHP refusing to parse the page. The quote on the end is the closing quote is it not? Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284640 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 This was what I needed. $_SESSION['order']['cartcontenttext'] = 'Small: '.$content['sizes'].' Medium: '.$content['sizem'].' Large: '.$content['sizel'].' Extra large: '.$content['sizexl'].''; Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284642 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 Why do you insist on adding extra quotes that are simply not needed? $_SESSION['order']['cartcontenttext'] = 'Small: '.$content['sizes'].' Medium: '.$content['sizem'].' Large: '.$content['sizel'].' Extra large: '.$content['sizexl']; What do you think the quotes on the end of your last post are actually for? They do nothing. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284644 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 Perhaps, but I have accomplished my desired outcome. That saying if it works don't change it (or something like that I'm too mind smacked), well, yeah that applies now. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284648 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 There is always room for code to be improved. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284649 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 I have more important things to do my friend, especially as I am 27, unemployed and still living at home. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284652 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 Yeah, that makes sense Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1284773 Share on other sites More sharing options...
phporcaffeine Posted November 4, 2011 Share Posted November 4, 2011 Defined by PHP refusing to parse the page. The quote on the end is the closing quote is it not? Closing quote for what? Here is some basic string handling 101: "Any text in these quotes will echo as typed, however the double quotes have the ability to also echo the defined values of variables (i.e $a would echo the value of $a in double quotes)" 'Any text in single quotes will echo as typed, and WILL not echo intrinsic value of typed variables, rather the literal value of the variable (i.e $a will echo as $a)' You only need a closing quote and apostrophe when there is a corresponding, opening quote or apostrophe. The PHP interpreter does not require any line terminating quotes or apostrophes. Generally, PHP lines are terminated with a semi-colon ";", except in some cases, where a statement may not complete on one line (i.e function/method/class declarations ... etc). You should really start by learning the what the difference is between strong and loose typed languages, how PHP is typed (loose as a goose) and then study up on PHP operators and basic control structures. The best way to help some one is to teach them how to find the answer themselves, not do it for them - that doesn't help at all. Quote Link to comment https://forums.phpfreaks.com/topic/250365-adding-onto-the-value-of-a-_session/#findComment-1285006 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.