gpt4 book ai didi

php - 从 session 数组生成逗号分隔的列表

转载 作者:行者123 更新时间:2023-11-29 14:14:41 25 4
gpt4 key购买 nike

我有一个 session 数组 [cart],其中包含用户 ID 号。我正在尝试从联系人表中生成电子邮件地址列表,其中 session 中的联系人 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 没有任何值(value),因为 if 语句被忽略了。

知道为什么会这样吗?

最佳答案

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

关于php - 从 session 数组生成逗号分隔的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12866259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com