Hyperjase Posted January 4, 2012 Share Posted January 4, 2012 I'm trying to get a foreach loop to one variable that I can insert into MySQL - I've tried a number of things but can't get it working. How do I create one single variable that is created from the foreach? Thanks! Jason Quote Link to comment Share on other sites More sharing options...
TOA Posted January 4, 2012 Share Posted January 4, 2012 foreach ($things as $thing) { $var .= $thing; } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 4, 2012 Share Posted January 4, 2012 implode Quote Link to comment Share on other sites More sharing options...
Hyperjase Posted January 4, 2012 Author Share Posted January 4, 2012 Thanks, not too sure what I need to do though, here is the code I have: <?php foreach($_SESSION['notopping'] as $key => $value) { $_SESSION['topping2'] = " $value x $key "; echo $_SESSION['topping2']; } ?> My problem is, if I try adding the topping2 session, it only shows the last variable spat out by the foreach, how do I send everything that is done within the foreach? Thanks, Jason[/code] Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 4, 2012 Share Posted January 4, 2012 You just use implode instead. Quote Link to comment Share on other sites More sharing options...
Hyperjase Posted January 4, 2012 Author Share Posted January 4, 2012 Thanks - I have this which I was helped with here a few days ago, but I have a problem. Within the array is a key and value, both of which I need to display. With this code it only shows the value, they key isn't showing. <?php $last = array_pop($_SESSION['notopping']); $topping = implode(', ', $_SESSION['notopping']). ' & ' .$last; echo $topping; ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 4, 2012 Share Posted January 4, 2012 Ah, didn't realize the key was also significant. The problem with your current foreach() loop is that you're overwriting the value of $_SESSION['topping2'] on each iteration, so that's why you end up with only the last value. So, change it to write the values to $_SESSION['topping2'][], and then use implode on that resulting array. Quote Link to comment Share on other sites More sharing options...
Hyperjase Posted January 4, 2012 Author Share Posted January 4, 2012 Right, I follow now but I can't get $_SESSION['topping2'][] to work - it says that it's not correct. Which bit of code are you basing this on? I tried different ways with both part of code I posted (I went for the foreach example as that's where you'd taken the session reference from. Should this all be done with no foreach loop? Thanks for your invaluable help! Jason Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 4, 2012 Share Posted January 4, 2012 Post the code you have currently, and I'll have a look at it. Quote Link to comment Share on other sites More sharing options...
Hyperjase Posted January 4, 2012 Author Share Posted January 4, 2012 Here's both (one I preferred using is commented out, as it added commas and ampersand, one that works is fine, just don't like output display!) <?php /* $last = array_pop($_SESSION['notopping']); $topping = implode(', ', $_SESSION['notopping']). ' & ' .$last; echo $topping; */ foreach($_SESSION['notopping'] as $key => $value) { $_SESSION['topping2'] = " $value x $key "; echo $_SESSION['topping2']; } ?> Thanks! Jason Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 4, 2012 Share Posted January 4, 2012 This should be what you're after, I believe. <?php foreach($_SESSION['notopping'] as $key => $value) { $_SESSION['topping2'][] = " $value x $key "; // add each pair to a new array element } $list = implode( ', ', $_SESSION['topping2'] ); // $list now contains a comma separated string of the "key x value" pairs echo $list; Quote Link to comment Share on other sites More sharing options...
Hyperjase Posted January 4, 2012 Author Share Posted January 4, 2012 Woohoo! Thanks - also made a slight modification using the other code, this now works exactly as I need it, also means I can input this straight to mysql! Fantastic, thanks for solving 4 days working of sleepless nights and working days spent not concentrating on what I'm supposed to be doing! <?php foreach($_SESSION['notopping'] as $key => $value) { $_SESSION['topping2'][] = " $value x $key "; // add each pair to a new array element } $last = array_pop($_SESSION['topping2']); $list = implode( ', ', $_SESSION['topping2']). ' & ' .$last; // $list now contains a comma separated string of the "key x value" pairs echo $list; } Thanks again! Jason Quote Link to comment 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.