0

我有一个会话数组 [cart],其中包含用户 ID 号。我正在尝试从联系人表中生成电子邮件地址列表,其中会话中的联系人 ID 号码 = 数据库中的联系人 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 语句被忽略了。

知道为什么会这样吗?

4

3 回答 3

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);
    }
于 2012-10-12T20:11:31.613 回答
0

$emailAry在外部作用域中声明数组,这样就可以从while和foreach之外的代码中访问它,就像他的:

$emailAry = array();
foreach ($_SESSION['cart'] as $key=>$val) {
...
...
于 2012-10-12T20:06:18.370 回答
0

使用它既简单又快速

 $to = '';
 foreach ($result->result() as $row){
    $to .=  $row->id.',';            
 }

 $to = rtrim($to,',');

根据你的改变它。

于 2013-11-13T22:00:01.293 回答