Forum Moderators: coopster

Message Too Old, No Replies

foreach loop problem

         

cookie2

12:24 am on Apr 15, 2006 (gmt 0)

10+ Year Member



I need a little help with a couple of foreach loops I'm trying to set up. I'm getting variables passed to a processing script from a form. The form includes several fields which are checkboxes and can have multiple selections. The data is passed to the processing script just fine and I'm trying to set up the printout of the data. I'm using the following:

foreach($_POST as $key => $value){
if (is_array($value)) {
echo "$key = ";
foreach ($value as $a) {
echo "<i>$a, </i>";
}
}else{
echo "$key = $value<br>";
}
}

which works fine but it puts the results of the arrays all on the same line like so:

firstArray = 2, 3, secondArray = 4, 6, 8, thirdArray - 5, 7

But what I need to do is put each array and it's result on a separate line like the non-array values are:

firstArray = 2, 3,
secondArray = 4, 6, 8,
thirdArray - 5, 7

How can I adapt my code to accomplish this? I've read a ton of PHP tutorial coding pages but none address the formatting of the results I seek.

Little_G

12:53 am on Apr 15, 2006 (gmt 0)

10+ Year Member



Hi,

Try this:

foreach ($value as $a) {
echo "<i>$a, </i>";
}
echo "<br>";
}
else{...

After the foreach has finished listing the array it prints a forced line-brake.

Andrew

cookie2

2:04 am on Apr 15, 2006 (gmt 0)

10+ Year Member



Perfect, thank you so much.

cookie2

3:32 am on Apr 15, 2006 (gmt 0)

10+ Year Member



One other related question that I hadn't noticed before.

When I use the foreach loop, I get all the variables passed by the form. How can I excluded selected ones from being processed by the foreach loop? I'm looking to not be printing out the $_POST[formname], $_POST[env_reports], etc onto the page output but can't figure out how to exclude them from the loop.

jatar_k

3:04 pm on Apr 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you would skip them inside the loop

if ($key == 'formname' ¦¦ $key == 'env_reports') continue;

or soething like that

Note: WebmasterWorld breaks pipes, ¦ is not a true pipe char and must be replaced with a true pipe char

cookie2

3:41 pm on Apr 15, 2006 (gmt 0)

10+ Year Member



Thank you jatar_k. Much obliged.