2012-10-28 55 views
0

我試圖發送一封電子郵件到多個電子郵件地址,這些電子郵件地址包含在數據庫中並被分類到一個記錄集中......記錄集有多列,但我只需要一個:「電子郵件」。我知道如果我把它們放在一個數組中,我可以用逗號分解它們,並用逗號分隔它們,但是我不知道如何使用記錄集列來做到這一點。任何人都知道嗎?我該如何內爆記錄集列?

這裏是記錄代碼:

$colname_rsAllLeads = "-1"; 
if (isset($_SESSION['MM_Username'])) { 
    $colname_rsAllLeads = $_SESSION['MM_Username']; 
} 
mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn); 
$query_rsAllLeads = sprintf("SELECT Email FROM Leads WHERE `User` = %s ORDER BY FullName ASC", GetSQLValueString($colname_rsAllLeads, "text")); 
$rsAllLeads = mysql_query($query_rsAllLeads, $myBackOfficeConn) or die(mysql_error()); 
$row_rsAllLeads = mysql_fetch_assoc($rsAllLeads); 
$totalRows_rsAllLeads = mysql_num_rows($rsAllLeads); 

NEW CODE:

$colname_rsAllLeads = "-1"; 
if (isset($_SESSION['MM_Username'])) { 
    $colname_rsAllLeads = $_SESSION['MM_Username']; 
} 
mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn); 
$query_rsAllLeads = sprintf("SELECT Email FROM Leads WHERE `User` = %s ORDER BY FullName ASC", GetSQLValueString($colname_rsAllLeads, "text")); 
$rsAllLeads = mysql_query($query_rsAllLeads, $myBackOfficeConn) or die(mysql_error()); 
$row_rsAllLeads = mysql_fetch_assoc($rsAllLeads); 
$totalRows_rsAllLeads = mysql_num_rows($rsAllLeads); 

$row_rsAllLeads = array(); 
while($row = mysql_fetch_assoc($rsAllLeads)) { 
    $row_rsAllLeads[] = $row; 
} 

$glued = ""; 
$separator = ""; 
foreach($row_rsAllLeads as $row) { 
    $glued .= $separator . $row['Email']; 
    $separator = ", "; 
} 

$editFormAction = $_SERVER['PHP_SELF']; 
if (isset($_SERVER['QUERY_STRING'])) { 
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); 
} 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) { 

    $startcode = $_POST['messagefield']; 
    $replaced = preg_replace('/\\\\(?="|\')/', '', $startcode); 
    echo $replaced; 
    $collectedleads = $row_rsAllLeads['Email']; 
    echo $collectedleads; 

    /* 
$to = $collectedleads; 
$subject = $_POST['subjectfield']; 
$body = $replaced; 
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$headers .= "From: " . $row_rs_CurrentUser['FirstName'] . " " . $row_rs_CurrentUser['LastName'] . " <" . $row_rs_CurrentUser['Email'] . ">"; 
if (mail($to, $subject, $body, $headers)) { 
    } else { 
    echo("<p>Message delivery failed...</p>"); 
    } 
    */ 

    $insertSQL = sprintf("INSERT INTO PendingEmails (`to`, subject, message) VALUES (%s, %s, %s)", 
         GetSQLValueString($row_rsAllLeads['Email'], "text"), 
         GetSQLValueString($_POST['subjectfield'], "text"), 
         GetSQLValueString($_POST['messagefield'], "text")); 

    mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn); 
    $Result1 = mysql_query($insertSQL, $myBackOfficeConn) or die(mysql_error()); 

    $insertGoTo = "Email Sent.php"; 
    if (isset($_SERVER['QUERY_STRING'])) { 
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
    $insertGoTo .= $_SERVER['QUERY_STRING']; 
    } 
    header(sprintf("Location: %s", $insertGoTo)); 
} 

回答

1

你需要自己膠水。 implode()在這裏沒有用處,因爲你必須遍歷你的記錄集,提取Email,放入單獨的數組,然後implode()。但既然你已經迭代,你可以W/O臨時陣列和implode()膠水,像這樣:

$glued = ""; 
$separator = ""; 
foreach($row_rsAllLEads as $row) { 
    $glued .= $separator . $row['Email']; 
    $separator = ", "; 
} 

編輯#1

你的代碼需要一個修復爲好,因爲你取剛第一個行集,不是所有匹配你的where子句。它應是這樣的(你應該從row_...rows_...改變變量名太):

$row_rsAllLeads = array(); 
while($row = mysql_fetch_assoc($rsAllLeads)) { 
    $row_rsAllLeads[] = $row; 
} 

因此所有$row_rsAllLeads後應該是數組的數組。

+0

我該怎麼做? – user1751581

+0

順便說一句,我添加了我的記錄集代碼... – user1751581

+0

請參閱編輯答案 –