...

View Full Version : Help - Including "if" statements into email $message



ghost2012
11-02-2009, 07:35 AM
How do I include and "if" statement into an email $message.

Example



<?php
$order01 = $_POST["order01"];
$order02 = $_POST["order02"];
$order03 = $_POST["order03"];

if ( $order01 > 0 ) {
echo " amount of $order01 <br />";
}

if ( $order02 > 0 ) {
echo " amount of $order02 <br />";
}

if ( $order03 > 0 ) {
echo " amount of $order03 <br />";
}
?>


All I'm trying to do is make sure that anyone that orders only receives the information that was selected not unneeded 0's or extra echo commands that print nothing as in
"amount of 0", or "amount of "nothing".

I'm new to this but I thought this would be simple. Including an if state from those above into the $message of an email.

Fou-Lu
11-02-2009, 08:08 AM
echo won't show up in an email unless you use output buffering and capture it as a variable.
First and foremost, use a dirty trick to get around problems with notice triggering in case you're _POST offsets are not available:


$order01 = &$_POST["order01"];
$order02 = &$_POST["order02"];
$order03 = &$_POST["order03"];

This way you won't need to change you're if statements to check for isset. Be aware that should you change $_POST it will be reflected in the given variables. I don't recommend using extract.
Now, capture you're output. I'll use an array:


$aOrder = array();
if ( $order01 > 0 ) {
$aOrder[] = " amount of $order01";
}

if ( $order02 > 0 ) {
$aOrder[] = " amount of $order02";
}

if ( $order03 > 0 ) {
$aOrder[] = " amount of $order03";
}

Mkay, now that we have that, you can implode the values and do whatever you want with them:


$to = 'mailto@domain.com';
$subject = 'My Subject';
$additionalHeaders = "From: myuser@mysite.com\r\n";

if (false !== @mail($to, $subject, implode("\n", $aOrder), $additionalHeaders))
{
print 'Mail Sent';
}
else
{
print 'Mail not sent via mail command.';
}


This assumes that you're order{DD} returns an integer representing how many items are ordered. It would be easier to send an html array for this, but I don't know what you're form intention is.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum