Jump to content

foreach() to mysql


Hyperjase

Recommended Posts

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]

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.