0

我有一个包含如下数据的数组:

01 | organization_1 | email_A | publication_A1 | pubID_A1
02 | organization_1 | email_A | publication_A2 | pubID_A2
03 | organization_1 | email_A | publication_A3 | pubID_A3
04 | organization_2 | email_B | publication_B1 | pubID_B1
05 | organization_2 | email_B | publication_B2 | pubID_B2
06 | organization_3 | email_C | publication_C1 | pubID_C1
07 | organization_4 | email_D | publication_D1 | pubID_D1
08 | organization_4 | email_D | publication_D2 | pubID_D2

数组的结构如下:

array('org' => organization_x, 'email' => email_x, 'title' => publication_x, 'pub_id' => pubID_x).

我需要做的是输出这个数组,以便它显示为单个电子邮件消息,每个消息后面都有一个提交按钮。查看此页面的人将查看显示的消息,并且他或她可以选择单击提交以将特定内容发送到适当的电子邮件。这是我们需要创建的初始显示:

<!-- Message #1 -->
<form>
<input type="hidden" name="pub_ids" value="pubID_A1;pubID_A2;pubID_A3">
<input type="hidden" name="recipient" value="email_A">
<input type="hidden" name="organization" value="organization_1">
<!-- start on-screen text display -->
To: email_A

Your account contains the following publication titles:

* publication_A1
* publication_A2
* publication_A3

You can edit these publications after logging into our system. Your email 
address, email_A, serves as your username.

Thanks!
<!-- end on-screen text display -->
<input type="submit" name="submit" value="submit">
</form>

<!-- Message #2 -->
<form>
<input type="hidden" name="pub_ids" value="pubID_B1;pubID_B2">
<input type="hidden" name="recipient" value="email_B">
<input type="hidden" name="organization" value="organization_2">
<!-- start on-screen text display -->
To: email_B

Your account contains the following publications:

* publication_B1
* publication_B2

You can edit these publications after logging into our system. Your 
email address, email_B, serves as your username.

Thanks!
<!-- end on-screen text display -->
<input type="submit" name="submit" value="submit">
</form>

...一直到数组中的所有项目都已处理完毕。

我已经尝试了以下代码的某些版本太久了。这个想法是遍历每个项目,将 pub_id 和 title 数据连接成文本字符串,以便通过表单传递或根据需要显示在屏幕上。当脚本遇到新的组织名称时,它应该将所有部分放在一起,然后开始处理下一个组织的电子邮件。

$current_org = '';
$pub_ids = '';
$titles = '';

foreach ($array AS $value)
{
    $pub_ids .= $value['pub_id'] . ';';
    $titles .= '* ' . $value['title'] . '<br />';
    if ($current_org != $value['org'])
    {
        echo '<form>';
        echo '<input type="hidden" name="pub_ids" value="' . $pub_ids . '">';
        echo '<input type="hidden" name="recipient" value="' . $value['email'] . '">';
        echo '<input type="hidden" name="organization" value="' . $value['org'] . '">';
        echo 'To: ' . $value['email'];
        echo '<br /><br />';
        echo 'Your account contains the following publication titles:';
        echo '<blockquote>';
        echo $titles;
        echo '</blockquote>';
        echo 'You can edit these publications after logging into our system. Your email address, ' . $value['email'] . ', serves as your username.';
        echo '<br /><br />';
        echo 'Thanks!';
        echo '<input type="submit" name="submit" value="submit">';
        echo '</form>';
        echo '<br /><br />';
        $pub_ids = '';
        $titles = '';
        $current_org = $value['org'];
    }
}

但我实际上得到的是,例如,pub_ids 看起来像这样:

第一条消息:<input type="hidden" name="pub_ids" value="pubID_A1">

第二条消息:<input type="hidden" name="pub_ids" value="pubID_A1;pubID_A2">

第三条消息:<input type="hidden" name="pub_ids" value="pubID_A1;pubID_A2;pubID_A3">

第 4 条消息:<input type="hidden" name="pub_ids" value="pubID_A1;pubID_A2;pubID_A3;pubID_B1">

第 5 条消息:<input type="hidden" name="pub_ids" value="pubID_A1;pubID_A2;pubID_A3;pubID_B1;pubID_B2">

编辑:每当脚本重新开始时添加 $pub_ids = '' 和 $titles = '' 。这现在反映在上面 foreach() 末尾附近的代码中。

所以现在问题似乎是第一个拥有多个出版物的组织只列出了一个出版物。然后,下一个组织会获得列出的最后一个组织的出版物以及他们自己的出版物。然后其余的被扔掉。与第一个 if() 语句有关吗?由于 $current_org 以 NULL 开始,第一个循环永远不会设置为 $value['org']。我不知道如何解决这个问题。我将在哪里/如何检查组织值是否已更改?

非常感谢您对这个问题的任何见解!

4

2 回答 2

1

我将此问题发布到 phpFreaks 和 StackOverflow。来自英国的 Barand 提供了一个了不起的答案,解决了我的问题。你可以在这里看到他的回复:http: //forums.phpfreaks.com/topic/279455-how-to-output-an-array-into-email-messages- contains-concatenated-data/ #entry1437523。我也在下面复制/粘贴。奇迹般有效。希望这对其他人有用。

Try:

$arr = array (
    array('org' => organization_w, 'email' => email_w, 'title' => publication_w1, 'pub_id' => pubID_w1),
    array('org' => organization_w, 'email' => email_w, 'title' => publication_w2, 'pub_id' => pubID_w2),
    array('org' => organization_w, 'email' => email_w, 'title' => publication_w3, 'pub_id' => pubID_w3),
    array('org' => organization_x, 'email' => email_x, 'title' => publication_x1, 'pub_id' => pubID_x1),
    array('org' => organization_x, 'email' => email_x, 'title' => publication_x2, 'pub_id' => pubID_x2),
    array('org' => organization_x, 'email' => email_x, 'title' => publication_x3, 'pub_id' => pubID_x3),
    array('org' => organization_y, 'email' => email_y, 'title' => publication_y1, 'pub_id' => pubID_y1),
    array('org' => organization_y, 'email' => email_y, 'title' => publication_y2, 'pub_id' => pubID_y2),
    array('org' => organization_z, 'email' => email_z, 'title' => publication_z, 'pub_id' => pubID_z)
);

$currOrg = '';
foreach ($arr as $data) {
    if ($currOrg != $data['org']) {
        if ($currOrg) {
            outputEmail ($currOrg, $currEmail, $titles, $ids);
        }
        $currOrg = $data['org'];
        $currEmail = $data['email'];
        $titles = $ids = array();
    }
    $titles[] = $data['title'];
    $ids[] = $data['pub_id'];
}
outputEmail ($currOrg, $currEmail, $titles, $ids);

function outputEmail($org, $email, $titles, $ids)
{
    // format your output here
    echo "Organisation: $org<br>";
    echo "Email: $email<br>";
    echo "Titles: " . join(', ', $titles) . '<br>';
    echo "Ids: " . join(', ', $ids) . '<hr>';
}
于 2013-06-24T12:41:48.350 回答
0

改变

$pub_ids .= $value['pub_id'] . ';';
$titles .= '* ' . $value['title'] . '<br />';

为了这:

$pub_ids = $value['pub_id'] . ';';
$titles = '* ' . $value['title'] . '<br />';

你必须删除连接

于 2013-06-21T21:43:10.290 回答