Brkonnia
Brkonnia

Reputation: 57

Displaying an array with foreach in a variable PHP

I've only skimmed through, but I wanted to get my first question out here, seeing as I haven't found a specific and similar example/inquiry.

I have on the 'first page' of a site many groups of varying companies. In each group are 1 - 20 or so check-boxed input fields so that a client can require different information be sent to them after they submit the form below. On a separate page that only runs a php script, I email the form to the host company. The message then displays as so: (and I changed content...)

I want it to read:

            *Favorite Animals: Dog, Cat, Bird*

However I have it reading:

            Favorite Animals: Dog,
            Favorite Animals: Cat,
            Favorite Animals: Bird,

My Code:

On the first page we have a list of different checkboxes in the same array with obvious different values... like shown below:

<input type="checkbox" name="animals[]" value="Dogs" class="cb">
 Dogs<br>
<input type="checkbox" name="animals[]" value="Cats" class="cb">
 Cats<br>
<input type="checkbox" name="animals[]" value="Birds" class="cb">
 Birds<br>
<input type="checkbox" name="animals[]" value="Dragons" class="cb">
 Dragons<br>

The above code is then submitted (via post) to a separate php document.

The relevant code in this second page is:

$animals = $_POST['animals'];

below...

    foreach ($animals as $an) {
$email_message .= "Favorite Animals: ".clean_string($an)." ,\n";
    }

I realize what's going on, but I haven't figured out a way to work the foreach statement so that the "Favorite Animal's" is displayed once, followed by the possible array of whatever was selected on the previous page.

I have no errors, only getting the information in a way I don't desire. Any help would be appreciated!

Upvotes: 2

Views: 317

Answers (5)

Barmar
Barmar

Reputation: 780724

Instead of using foreach and dealing with the extra comma yourself, use implode.

$email_message = implode(', ', array_map('clean_string', $animals);

Upvotes: 1

janenz00
janenz00

Reputation: 3310

Just take the static parts outside the loop. That should do it.

 $email_message .= "Favorite Animals: ";

 foreach ($animals as $an) {
   $email_message .= clean_string($an)." ,";
 }
  $email_message = rtrim ($email_message, ",");

 $email_message .= "\n";

 // Continue appending rest of the email message

Upvotes: 0

anditpainsme
anditpainsme

Reputation: 649

The other answers are much better, but just some food for thought you could also achieve this with conditional statements like this:

$animals = array('Animal 1', 'Animal 2', 'Animal 3');
$email_message = "";
$firstTime = true;
foreach ($animals as $an) {
    if ($firstTime === true) {
        $email_message .= "Favorite Animals: ".$an." ,\n";
        $firstTime = false;
    } else {
        $email_message .= $an." ,\n";
    }
}

$email_message = substr($email_message, 0, -3);
echo $email_message;

Upvotes: 0

philwinkle
philwinkle

Reputation: 7056

What's happening here is that the form is only posting what was selected, and not what wasn't selected. What you probably want is for it to display a list of all possible values of animals along with their selection. To do this I would change your submit form and let it post with all values:

<input type="checkbox" name="animals['Dogs']" onclick="this.value='yes'" value="no" class="cb">
 Dogs<br>
<input type="checkbox" name="animals['Cats']" onclick="this.value='yes'" value="no" class="cb">
 Cats<br>
<input type="checkbox" name="animals['Birds']" onclick="this.value='yes'" value="no" class="cb">
 Birds<br>
<input type="checkbox" name="animals['Dragons']" onclick="this.value='yes'" value="no" class="cb">
 Dragons<br>

And your email print function should be updated to include the keys in the array now:

$email_message = "Favorite Animals:";
foreach ($animals as $key=>$val) {
  $email_message .=".clean_string($key). "=" . clean_string($val) . " ,\n";
}

So what is happening is that all values are being passed, but they're changing from 'no' to 'yes' when someone clicks on them. This is a workaround, there are other ways of handling it, but they may require updating more than just your form.

Upvotes: 0

GajendraSinghParihar
GajendraSinghParihar

Reputation: 9131

Initialize the string $email_message with "Favorite Animals: " and then just append the rest of the array in loop

$email_message = "Favorite Animals: ";
 foreach ($animals as $an) {
$email_message .= clean_string($an)." , ";
    }

Upvotes: 0

Related Questions