2013-05-17 63 views
0

我有这个脚本来发送包含来自我的数据库的信息的电子邮件。用户可以在数据库中拥有1+项目的位置。因此,当我清空与用户匹配的行时,发送的电子邮件数量就等于他们拥有的行数。所以如果他们在数据库中有8个项目,它会发送8封电子邮件。每个添加一个项目。所以第一封邮件有一个项目,第二个邮件有两个项目,依此类推。我正在试图找到一种简单的方法,让它在发送电子邮件之前获取所有信息,以便客户只收到一封电子邮件。合乎逻辑的方法是回显信息,但我不能用php变量来做到这一点。我没有在下面的代码中包含查询和数据库连接。任何帮助将被爱。Php电子邮件Mysql获取数组

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); 
} 
+1

为什么'$ Items'级联? – Fabio

+0

不带“。”它只发送数据库中的一个项目。看起来必须包含该点才能发送多个项目。我还想到了尤金。感谢任何回复帮助的人。 –

+0

你应该开始它与正常平等,然后连接,否则将无法正常工作 – Fabio

回答

1

只是移动发送功能进行循环:

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); 
} 
+0

工作很好。我很感激帮助。 –

+2

@BobStone你应该标记答案是正确的,给他他的代表。 – Kevin

0

当你迭代的项目,你应该只建立一条消息,而不是整个电子邮件..这是很难做的比下面更不知道更多关于您的查询。无论如何,我会试一试:

$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); 

注:的是,我不会实现这个代码。它的意图是作为关于如何构造代码的一个例子。

0

concatenation是不对的,你应该首先声明的是空变量的循环

$Items = ''; 

之外然后开始你的loop并获取所有数据你需要连接你的变量

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

现在您已准备好发送电子邮件,outside your loop或者您最终将为每个cicle发送一封电子邮件。所以,你的代码看起来像这样

$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); 

那么我想你要记住,mysql_*功能已被弃用,所以我会建议你切换到mysqliPDO