While Loop inside email message

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
netpro25
Forum Newbie
Posts: 11
Joined: Wed Sep 21, 2005 4:47 pm

While Loop inside email message

Post by netpro25 »

I want to put a while loop inside an email message so I dont have to list out all of the variables, and plus the variables are conditional so they dont always contain data.

Code: Select all

$message = "Hello thanks for your purchase here is your information $variable1 $variable2 $variable3";

How can I do this?

I hope this is clear.

thanks,
Marcel
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

email = text only. I hope you mean loop through the variables and parse them into the message variable. :P

Anyway, you can't loop through random variables. You can only loop through an array. If your variables were as such:

Code: Select all

$variables = array('dataone','datatwo','datathree');
//or
$variables[0] = 'dataone';
$variables[1] = 'datatwo';
//...
then just loop through as so:

Code: Select all

$message = "Hello..blah,blah,blah....\n\n";
for ($i=0; $i < count($variables); $i++) {
  if (!empty($variables[$i])) $message .= $variables[$i]."\n";
}
If you want to store the name of the field as well, either use two arrays or key your data as so:

Code: Select all

$variables = array(
  'nameone' => 'dataone',
  'nametwo' => 'datatwo',
  //...
);
//or
$names = array('nameone','nametwo','namethree');
$variables = array('dataone','datatwo','datathree');
Then loop through them as so:

Code: Select all

foreach ($variables as $name => $data) {
  $messages .= $name . ": " . $data;
}
//or
for ($i=0; $i < count($names); $i++) {
  if (!empty($variables[$i])) $message .= $names[$i] . ': ' . $variables[$i]."\n";
}
If you fetch data from a database, on the other hand...

Code: Select all

$result = mysql_query("SELECT * FROM `table_name`");
while ($row = mysql_fetch_assoc($result)) {
  $messages .= $row['variableone'] . $row['variabletwo']; //...
}
mysql_fetch_assoc() returns string keys, mysql_fetch_row() returns numerical keys, and mysql_fetch_array() returns both.


I hope that someone answers your quite ambiguous question.
netpro25
Forum Newbie
Posts: 11
Joined: Wed Sep 21, 2005 4:47 pm

Post by netpro25 »

Here is the code that I have...

Code: Select all

if ($quantity > 1) {

	//used is not equal to zero
	$sql="SELECT * FROM keymail WHERE used != '0' LIMIT '$quantity'";
	$query=mysql_query($sql);
	$result=mysql_fetch_object($query);

$message="Dear $first_name, \n \n Thank you for your order. We have received your payment and have processed your order, please do not disregard this email as it will serve as your receipt and also contains your software key code. \n \n Order Details:\n Description: $item_name \n Price: $payment_amount \n \n Instructions: \n 1. Download the Norman Installer from the following location: http://www.nothing.com \n 2. After running the installer you will be prompted to enter the key code, please copy and paste the following key code: \n \n $result->key_code \n \n 3. For further information on using and installing Norman Antivirus please refer to the manual: http://download.norman.no/manuals/eng/N ... de_eng.pdf \n \n This key is only valid for use on a single PC. \n \n Thank you again, \n The Nexgen Team \n \n";

		$i=0;
		while ($i < $quantity) {

		$kode=mysql_result($result,$i,"key_code");

		$kode$i = $kode

		$i++;
			}

	}

So i Have a database that has key codes in it, customers purchase these key codes and if they buy more then one then the code above runs, if not a seperate part runs. I need to be able to email my customers multiple key codes within one email from multiple rows in a table, which if i wanted to print this on the screen I usually use the while loop, so thats what I wanted to use for the email, I am sure someone knows of an easier way to do this. I hope this clearifys....

Thanks,
Marcel
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Just some advice, the way I do emails is to split up the lines on separate lines. Kinda makes sense, eh? :P

Code: Select all

$message="Dear $first_name,\n\n".
         "Thank you for your order. We have received your payment and have processed your order, please do not disregard this email as it will serve as your receipt and also contains your software key code.\n\n".
         "Order Details:\n".
         "Description: $item_name\n".
         "Price: $payment_amount\n\n".
         "Instructions:\n".
         "1. Download the Norman Installer from the following location: http://www.nothing.com\n".
         "2. After running the installer you will be prompted to enter the key code, please copy and paste the following key code:\n\n".
         "$result->key_code\n\n".
         "3. For further information on using and installing Norman Antivirus please refer to the manual: http://download.norman.no/manuals/eng/N ... de_eng.pdf\n\n".
         "This key is only valid for use on a single PC.\n\n".
         "Thank you again,\n".
         "The Nexgen Team\n\n";
Anyway, just split it up like so:

Code: Select all

$result = mysql_query("SELECT * FROM keymail WHERE used != '0' LIMIT '$quantity'");

$message="Dear $first_name,\n\n".
         "Thank you for your order. We have received your payment and have processed your order, please do not disregard this email as it will serve as your receipt and also contains your software key code.\n\n".
         "Order Details:\n".
         "Description: $item_name\n".
         "Price: $payment_amount\n\n".
         "Instructions:\n".
         "1. Download the Norman Installer from the following location: http://www.nothing.com\n".
         "2. After running the installer you will be prompted to enter the key code, please copy and paste the following key code:\n\n";

while ($row = mysql_fetch_assoc($result)) {
  $message .= $row['keycode']."\n";  // no idea how you store your information here.
}

$message .= "\n".
            "3. For further information on using and installing Norman Antivirus please refer to the manual: http://download.norman.no/manuals/eng/N ... de_eng.pdf\n\n".
           "This key is only valid for use on a single PC.\n\n".
           "Thank you again,\n".
           "The Nexgen Team\n\n";
Don't know exactly how you want it done, but if description, price, etc are all in the same table, or what. If they're in the same table, just move those lines inside the loop. If they're in a different table, you'll need to either get all the data from one table first or you can do a JOIN in the sql.

A note on your while loop... Change it to a foor loop and use arrays.

Code: Select all

for ($i=0, $kodes=array(); $i < $quantity; $i++) {
  $kodes[$i] = mysql_result(($result,$i,"key_code");
}
Or, if you want to spread it out a bit...

Code: Select all

$kodes = array();
for ($i=0; $i < $quantity; $i++) {
  $kodes[$i] = mysql_result(($result,$i,"key_code");
}
Whichever. ;)
Post Reply