Forum Moderators: coopster

Message Too Old, No Replies

Loops in PHP email

I need to run a loop in the $message part of a php email...

         

charger9

4:10 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



I'm trying to piece together an email that sends php variables. I need to run a loop in the $message, with no luck so far. In my head this is how it works...

<? php
session_start();
for($i=1;$<=$_SESSION['total'];$i++) {
$_SESSION['id-' . $i]=$_POST['id-' . $i];
$_SESSION['ad-' . $i]=$_POST['ad-' . $i];
}

$message=
"<html>
<body>
<table>
<tr>
<td>" .
for($i=1;$<=$_SESSION['total'];$i++) {
$_SESSION['id-' . $i] .
$_SESSION['ad-' . $i] .
} . "
</td>
</tr>
</table>
</body>
</html>";

mail( "email@address.com", $message );
?>

There are more variables, but this is the guts of it. Am I way off?

andrewsmd

4:26 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to run the loop outside of $message, you can't set it within message. I can't really tell what exactly your trying to do so paste more code if this doesn't help but do something like this.
$message = "<html><body><table><tr><td>";
for($i=1; $i<=$_SESSION['total']; $i++){

$message .= $_SESSION['id-' . $i];
$message .= $_SESSION['ad-' . $i];

}//for
$message .= "</td></tr></table></body></html>";
That should store what you need. Let me know if you need more help.

charger9

6:05 pm on Sep 10, 2008 (gmt 0)

10+ Year Member



What you said makes sense. I copied the code above, and the variables didn't print. I have a form with a loop that allows you to select multiples of the same product. I want to print those selections in this email.

Here's my code after your suggestions:

<?php
session_start();
$message =

"<html>
<head>Request</head>
<body>

<table>
<tr>
<td>Name:</td>
<td>" . $_SESSION['Name'] . " " . $_SESSION['Email'] . " " . $_SESSION['Phone'] . "</td>
<td>&nbsp;</td>
</tr>
<tr>
<td></td>
<td valign='top'>" . $_SESSION['plan'] . "<br>";

for($i=1;$i<=$_SESSION['total'];$i++) {

$message .= $_SESSION['id-' . $i];
$message .= $_SESSION['fee-' . $i];
$message .= $_SESSION['msg-' . $i];
$message .= $_SESSION['eg-' . $i];
$message .= $_SESSION['da-' . $i];
$message .= $_SESSION['eq-' . $i];
$message .= $_SESSION['wi-' . $i];
$message .= $_SESSION['ce-' . $i];
$message .= $_SESSION['wir-' . $i];

}

$message .= "</td>
<td>&nbsp;</td>
</tr>
</table>
</body>
</html>"
;

$headers = "MIME-Version: 1.0" . "\r\n";
$headers = $headers . "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers = $headers . "From: address@email.com";
mail( "address@email.com", "Request", $message, $headers );
header( "Location: http://www.example.com" );

?>

[edited by: dreamcatcher at 6:17 pm (utc) on Sep. 10, 2008]
[edit reason] use example.com. Thanks. [/edit]

andrewsmd

7:29 pm on Sep 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working.

charger9

1:05 pm on Sep 11, 2008 (gmt 0)

10+ Year Member



It's not. Can you help?

andrewsmd

1:43 pm on Sep 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh sorry, well you need to tell me what is not working. Is your e-mail part not working, or is the message still wrong. It's hard for me to debug this because I still don't see where you are getting your variables from. Could you possibly paste the code that stores all of these session variables. Also to a var_dump($variable); on all of your variables including the session variables and give me the output. Make sure to do the var_dump on the session variables in the for loop.