我有一个会话数组[cart],其中包含用户ID号。我正在尝试从联系人表格中生成电子邮件地址列表,其中会话中的联系人ID号码=数据库中的contact_id。
这是我的代码。
// Find cart members from the session array
foreach ($_SESSION['cart'] as $key=>$val) {
$contactid = $val;
// Query the database for cart members
$cartresult = mysql_query("SELECT contact.email FROM contact WHERE contact.contact_id = '$contactid'");
$emailresult = mysql_query($emailquery) or DIE (mysql_error());
while ($r = mysql_fetch_array($emailresult)) {
$emailAry[] = $r['email'];
}
}
// Create comma separated list from email array
if (count($emailAry)) {
$list = implode(", ", $emailAry);
$list = str_replace(" ,", "", $list);
显然$ emailAry没有价值,因为if语句被忽略了。
知道为什么会这样吗?
答案 0 :(得分:1)
$emailquery
从我的角度来看是未定义的。
$emailAry = array();
// Find cart members from the session array
foreach ($_SESSION['cart'] as $key=>$val) {
$cartresult = mysql_query("SELECT email FROM contact WHERE contact_id = '$val'");
while ($r = mysql_fetch_array($cartresult)) {
$emailAry[] = $r['email'];
}
}
// Create comma separated list from email array
if (count($emailAry)) {
$list = implode(",", $emailAry);
// no need to replace ', ' with '', implode will do all the job creating a CSV :)
//$list = str_replace(" ,", "", $list);
}
答案 1 :(得分:0)
在外部作用域中声明$emailAry
数组,因此您可以从while和foreach中的代码中访问它,就像他的:
$emailAry = array();
foreach ($_SESSION['cart'] as $key=>$val) {
...
...
答案 2 :(得分:0)
使用它简单快捷
$to = '';
foreach ($result->result() as $row){
$to .= $row->id.',';
}
$to = rtrim($to,',');
根据你的情况改变它。