homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

need help to separate variables in a foreach loop
can you help please

 10:21 am on Jul 7, 2006 (gmt 0)

Hello all,

Hopefully you will find this an easy one. I am giving users a selection of checkboxes in a form that they can select. I want to collect the information from the responses to store in a database.

Here is some code which seperates out the reasons given and lists them to the screen:

if(isset($_POST['reasons'])) {
foreach(($_POST['reasons']) as $reasongiven => $Arrayitem) {
echo "$Arrayitem <br>";

Can anyone please tell me how to also set a new variable to each item that is being echoed to the screen?

I want to echo the item, but also set a variable to contain this information so that I can then pass the variables into a database and send them via email.

Thank you for helping



 10:31 am on Jul 7, 2006 (gmt 0)

Why would you want to create a new variable for each value? The information is already in an array($_POST['reasons']), so you can just use that to insert into a db.


 10:32 am on Jul 7, 2006 (gmt 0)

That only gives me the last variable entered though - I need to collect all of them...


 10:40 am on Jul 7, 2006 (gmt 0)

I'm not sure I understand your comment. Do you have the checkboxes in an array--meaning is the name the same on each checkbox with '[]' at the end? If not, this is what you are looking to do. Something like this in the form:

Just because<input type='checkbox' name='reasons[]' value='just because' /><br />
No reason<input type='checkbox' name='reasons[]' value='none' />


 10:41 am on Jul 7, 2006 (gmt 0)

Yes they all have the name reasons[] and there are 14 choices that can be selected...


 10:57 am on Jul 7, 2006 (gmt 0)

...So then the info is in an array and you don't need new variables to achieve what you want. Look at what this shows you:

echo '<pre>';
echo '</pre>';


 11:00 am on Jul 7, 2006 (gmt 0)

Hi cazgh

eelixduppy is right and you could get the whole array into the DB directly from the $_POST array.

The most obvious way (to me) is to use the implode() function to create an SQL friendly list of comma separated values under 1 new variable name.

So something like : -

if(isset($_POST['reasons'])) {
$imploded = implode(",",$_POST['reasons'])

this would create a variable called $imploded with a value of 'value1, value2, value3, value4' which could then be used as a part of an SQL statement - of course, you still need to construct the rest of the SQL statement to make use of $imploded ;-)

Hope this helps, Rolf


 11:00 am on Jul 7, 2006 (gmt 0)

OK - but still not sure how to send them through via email - sorry, you'll have to excuse me as I am a bit thick, and even worse on Friday's as my brain is generally mashed by this time of the week...


 11:04 am on Jul 7, 2006 (gmt 0)

>>>send them through via email

For that, you can refer to this thread on Basics of Submitting and Emailing Forms with PHP [webmasterworld.com] taken from our Library [webmasterworld.com]. Good luck ;)


 12:09 pm on Jul 7, 2006 (gmt 0)


That works brilliantly - thank you both for taking the time to offer your help.

Much appreciated!


 12:52 pm on Jul 7, 2006 (gmt 0)

My pleasure :-)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved