Avatar of JohnMac328
JohnMac328Flag for United States of America

asked on 

PHP foreach syntax

I am having trouble with the syntax of this code - I want to list several values such as name - email etc below the $Body.  Do I have to list $Body several times to get the different values?  Or can I concatenate the values with a <br>

foreach($_POST as $key => $value) {
                $Body .= "<p>$key: $value</p>";
            }

Open in new window


THanks
PHPMySQL ServerWeb Development

Avatar of undefined
Last Comment
JohnMac328
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

As written that code will assign the variable $Body with the POST request vars.  After that, use echo to print $Body.
Avatar of Julian Hansen
What do you want the code to do that it is not already doing?
The code as you have it will create a new line for each key value pair - what is it you are wanting?
Avatar of JohnMac328
JohnMac328
Flag of United States of America image

ASKER

I want to list the name, email, company name etc - I was thinking that I had to specify the email field name
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

The name field will be the $key variable.
Avatar of JohnMac328
JohnMac328
Flag of United States of America image

ASKER

I guess I am trying to figure out where to place the echo statement since I can't find an example with my format
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Here's a self-contained example.  You may find var_dump() to be very helpful!
<?php // demo/temp_johnmac328.php
/**
 * http://www.experts-exchange.com/questions/28937729/PHP-foreach-syntax.html
 */
error_reporting(E_ALL);

// IF THE FORM HAS BEEN POSTED
if (!empty($_POST))
{
    // SHOW THE CONTENTS OF THE REQUEST
    var_dump($_POST);

    // ITERATE OVER THE CONTENTS OF THE REQUEST
    $thing = NULL;
    foreach ($_POST as $key => $val)
    {
        $thing .= "<p>$key: $val</p>";
    }
    echo $thing;
}

// CREATE THE FORM USING HEREDOC NOTATION
$form = <<<EOD
<form method="post">
Name:  <input name="yourname" /><br>
Email: <input name="youremail" /><br>
Phone: <input name="yourphone" /><br>
<input type="submit" value="Go!" />
</form>
EOD;

echo $form;

Open in new window

Avatar of JohnMac328
JohnMac328
Flag of United States of America image

ASKER

Like this?

The mail server is not up yet

foreach($_POST as $key => $value) {
                $Body .= "<p>$key: $value</p>";
                echo $key['name'], " ", $$key['company_name'], " ", $$key['email'];
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of JohnMac328
JohnMac328
Flag of United States of America image

ASKER

Thanks for the info
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo