0

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

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

You my friend, are a star! Thanks

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.