Dan
Dan

Reputation: 997

Running a Loop through $message in mail()

This is a follow on to this question.

I'm putting together a simple HTML email confirming an order to my database. As each order is obviously dynamic, i need part of $message to run through a loop.

For example i query my database outside of the $message and end up with

$emailinfo=mysql_fetch_assoc($result) or die(mysql_error()); 

I begin my $message...

$message = <<<END
<html>
  <head>
    <title>Whatever</title>
  </head>
  <body>
    <p>{$emailinfo['itemname']}</p>
  </body>
</html>
END;

The above is fine if someone has only ordered one item, but what i need to do is account for if someone orders more than one item, looping through each one and echoing out in $message. Outside of $message i can do this (which works)

do {
echo $emailinfo['itemname'];
} 
while ($emailinfo=mysql_fetch_assoc($result));

But when i wrap my $message within the loop, as was suggested in the comments on the previous question, it still only echos out the first row/order. E.g.

do {
$message = <<<END
<html>
  <head>
    <title>Whatever</title>
  </head>
  <body>
    <p>{$emailinfo['itemname']}</p>
  </body>
</html>
END;
} 
while ($emailinfo=mysql_fetch_assoc($result));

Can someone help? Outside of $message, the loop, query etc work fine, i just need it to work within $message. And it's only part of the order i need to loop through. I don't need to loop through customer info, delivery address etc as there'll only be one of those (if that helps at all).

Thanks as always

Upvotes: 0

Views: 765

Answers (1)

Jared
Jared

Reputation: 12524

what's happening here is that you are overwriting the $message variable with each iteration of the loop.

    $message = <<<END
<html>
  <head>
    <title>Whatever</title>
  </head>
  <body>
END;

do {
$message .= <<<END
    <p>{$emailinfo['itemname']}</p>
END;
} 
while ($emailinfo=mysql_fetch_assoc($result));

$message .= <<<END
  </body>
</html>
END;

Upvotes: 1

Related Questions