2

I have a php form that allows the user to add multiple copies of three fields (installed item, description, and quantity). The results of each field are captured in an array.

I am sending the results via an email form. Currently I have it set to the following:

$email_body = "Date: $date \n".
   "Installed Items: \n" . implode("\n", $_POST['installed']) . "\n" .
   "Installed Description: \n" . implode("\n", $_POST['installed-description']) . "\n" .
   "Installed Quantity: \n" . implode("\n", $_POST['installed-quantity']) . "\n";

It displays like this in the email:

Installed Items:

Item 1
Item 2
Item 3

Installed Description:

Description 1
Description 2
Description 3

Installed Quantity:

Quantity 1
Quantity 2
Quantity 3

I would like for it to display as follows:

Item 1
Description 1
Quantity 1

Item 2
Description 2
Quantity 2

etc...

Any help would be appreciated (and, I'm pretty new to PHP so pardon the noob-ness of this question).

1 Answer 1

1

In this case I would recommend to use a for loop:

for($i = 0; $i < COUNT($_POST['installed']);$i++)
{
   $msg .= 'Item '.$i.'\n';
   $msg .=  $_POST['installed'][$i].'\n';
   $msg .=  $_POST['installed-description'][$i].'\n';
   $msg .=  $_POST['installed-quantity'][$i].'\n';
   $msg .= '\n'; //extra space between the items
}
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks @ofir-baruch! This looks like exactly what I was looking for. However, I'm getting a syntax error when I drop it into the $email_body section of my code.
I'm getting a 500 error in my browser and Dreamweaver (i know, i know) is giving me a general message that I have a syntax error on the first line.
$email_body = "Date: $date \n". for($i = 0; $i < COUNT($_POST['installed']);$i++) { $msg .= 'Item '.$i.'\n'; $msg .= $_POST['installed'][$i].'\n'; $msg .= $_POST['installed-description'][$i].'\n'; $msg .= $_POST['installed-quantity'][$i].'\n'; $msg .= '\n'; //extra space between the items } $to = "[email protected]";
sorry...don't know how to paste that so it looks nice.
You forgot semicolon in this part:$email_body = "Date: $date \n". Instead of this one , write: $email_body = "Date: $date \n";
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.