Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this script to send an email containing information from my database. The user can have 1+ items in the database with it's location. So when I empty the rows that match the user the amount of emails sent equals the number of rows they have. So if they have 8 items in the database it send 8 emails. Each adds an item. So the first email has one item, the second with two items, and so on. I am trying to find a simple way to make it get all the information before sending the email so the customer only gets one email. The logical way would be to echo the information but I can't do that with a php variable. I didn't include the query and database connection in the code below. Any help would be loved.

while ($row = mysql_fetch_array($query)) {
$Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
$to = "[email protected]";
$from = "[email protected]";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: [email protected]";
mail($to, $subject, $message, $headers);
}
share|improve this question
1  
Why is $Items concatenated? –  Fabio May 17 '13 at 17:18
 
With out the "." it only sends one of the items in the database. It appears that the dot must be included to send multiple items. Also I figured it out thanks to Eugene. Thanks to anyone that replied with help. –  Bob Stone May 17 '13 at 17:28
 
You should start it with a normal equal and then concatenated otherwise it won't work correctly –  Fabio May 17 '13 at 17:29
add comment

3 Answers

up vote 1 down vote accepted

Just move send function out of cycle:

while ($row = mysql_fetch_array($query)) {
     $Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";

 }
 if ($Items != '') {
      $to = "[email protected]";
      $from = "[email protected]";
      $subject = "Test";
      $message = "$Items";
      $headers = "MIME-Version: 1.0 \r\n";
      $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
      $headers .= "From: [email protected]";
      mail($to, $subject, $message, $headers);
 }
share|improve this answer
 
Worked great. I appreciate the help. –  Bob Stone May 17 '13 at 17:26
2  
@BobStone You should flag the answer as correct to give him his rep. –  Kevin May 17 '13 at 23:13
add comment

When you iterate over the items, you should only build a message and not the entire email.. It's hard to do much more than the following without knowing more about your query. I'll give it a shot anyway:

$message = '';

while ($row = mysql_fetch_array($query)) {
     $Items =  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
     $message .= "$Items";
}

$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: [email protected]";
mail($to, $subject, $message, $headers);

Note: I wouldn't implement this code as is. It's meant to serve as an example on how to structure your code.

share|improve this answer
add comment

Your concatenation is wrong, you should first declare your variable as empty outside your loop

$Items = '';

Then start your loop and get all data you need concatenating your variable

while ($row = mysql_fetch_array($query)) 
{
    $Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
}

Now you are ready for send email, outside your loop or you will end up with one email for each cicle. So your code would look like this

$Items = '';
while ($row = mysql_fetch_array($query)) 
{
    $Items .=  $row['Items']. " - Aisle " .$row['Loc']. "<p>&nbsp;</p>";
}
$from = "[email protected]";
$subject = "Test";
$message = "$Items";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "From: [email protected]";
mail($to, $subject, $message, $headers);

Then I would like you to remember that mysql_* functions are deprecated so i would advise you to switch to mysqli or PDO

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.