Jump to content

How to output an array into email messages containing concatenated data?

Go to solution Solved by Barand,

Recommended Posts

I have an array with data that looks like this:
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
The array is structured like this:
array('org' => organization_x, 'email' => email_x, 'title' => publication_x, 'pub_id' => pubID_x)
What I need to do is output this array so that it displays as individual email messages with a submit button after each. A person viewing this page would review the displayed message and he or she would have the option to click submit to send a particular message to the appropriate email. Here's the initial display I need to create:
<!-- Message #1 -->
<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.

<!-- end on-screen text display -->
<input type="submit" name="submit" value="submit">
<!-- Message #2 -->
<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.

<!-- end on-screen text display -->
<input type="submit" name="submit" value="submit">
...and on until all of the items in the array have been processed.
I've tried some version of the following code for too long now. The idea is to loop through each item, concatenating the pub_id and title data into text strings to be passed through the form or shown on screen as needed. When the script hits a new organization name, it should put all the pieces together and then get started on the next organizaton's email message.

The Code:

$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'];
The Problem: 
The problem seems to be the first organization with more than one publication only gets one publication listed out. Then the next organization gets the last org's publication listed as well as their own. Then the rest are thrown off. Thinking this has something to do with my "if ($current_org != $value['org'])". Since $current_org starts off as NULL the first loop will never be set to $value['org'], so I get one loop through that gives me a pub_id, a title, and an email message -- then the old_org "changes" from NULL to the first $value['org'] value and the loop starts over again. I think this is what is going on. I can't figure out how to fix this though. Where/how would I check to see if the org value has changed?
Any insight into this problem is much appreciated!


Link to comment
Share on other sites


$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>';

Link to comment
Share on other sites

Hi Barand, and thanks for your code. I tried it and I get no output at all. I think this is because $currOrg never gets a chance to get set:

$currOrg = ''; // set to nothing here
foreach ($arr as $data) {
    if ($currOrg != $data['org']) { // $currOrg isn't set to $data['org'] since it is still set to nothing
        if ($currOrg) { // currOrg is still set to nothing, so this if never executes
            outputEmail ($currOrg, $currEmail, $titles, $ids);

I tried switching this around to start with if ($currOrg)..., but since $currOrg always starts out as being set to nothing, again the if statement never executes.


Thanks again for your go at this. Any other ideas are welcome!


Link to comment
Share on other sites

  • Solution

In my code, $currOrg gets set on the line after the part you just posted. That code was tested prior to posting.


output was

Organisation: organization_w
Email: email_w
Titles: publication_w1, publication_w2, publication_w3
Ids: pubID_w1, pubID_w2, pubID_w3
Organisation: organization_x
Email: email_x
Titles: publication_x1, publication_x2, publication_x3
Ids: pubID_x1, pubID_x2, pubID_x3
Organisation: organization_y
Email: email_y
Titles: publication_y1, publication_y2
Ids: pubID_y1, pubID_y2
Organisation: organization_z
Email: email_z
Titles: publication_z
Ids: pubID_z
Edited by Barand
Link to comment
Share on other sites

Sorry - I rushed through setting up your code within what exists in my script. I screwed up the implementation. Your code works great! Thanks so much for taking the time to respond, and then to respond again. Really appreciate your help and expertise!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.