Welcome to WebmasterWorld Guest from 54.226.183.49

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

need help to separate variables in a foreach loop

can you help please

     

cazgh

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

10+ Year Member



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

eelixduppy

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

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



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.

cazgh

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

10+ Year Member



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

eelixduppy

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

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



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' />

cazgh

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

10+ Year Member



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

eelixduppy

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

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



...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>';
[url=http://us3.php.net/print_r]print_r[/url]($_POST['reasons']);
echo '</pre>';

Rolf21

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

5+ Year Member



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

cazgh

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

10+ Year Member



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...

eelixduppy

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

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member



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

cazgh

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

10+ Year Member



Rolf21

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

Much appreciated!

Rolf21

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

5+ Year Member



My pleasure :-)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month