Reputation: 173
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 to the appropriate email. Here's the initial display we need to create:
<!-- 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>
...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.
$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'];
}
}
But what I'm actually getting is, for example, pub_ids that look like this:
1st message: <input type="hidden" name="pub_ids" value="pubID_A1">
2nd message: <input type="hidden" name="pub_ids" value="pubID_A1;pubID_A2">
3rd message: <input type="hidden" name="pub_ids" value="pubID_A1;pubID_A2;pubID_A3">
4th message: <input type="hidden" name="pub_ids" value="pubID_A1;pubID_A2;pubID_A3;pubID_B1">
5th message: <input type="hidden" name="pub_ids" value="pubID_A1;pubID_A2;pubID_A3;pubID_B1;pubID_B2">
Edit: Added $pub_ids = '' and $titles = '' whenever the script starts over. This is now reflected in the code above near end of the foreach().
So NOW 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. Something to do with the first if() statement? Since $current_org starts off as NULL the first loop will never be set to $value['org']. I can't figure out how to fix that though. Where/how would I check to see if the org value has changed?
Any insight into this problem is much appreciated!
Upvotes: 0
Views: 133
Reputation: 173
I posted this question to phpFreaks as well as StackOverflow. Barand from the UK supplied a terrific answer, resolving my issues. You can see his response here: http://forums.phpfreaks.com/topic/279455-how-to-output-an-array-into-email-messages-containing-concatenated-data/#entry1437523. I've copy/pasted below as well. Works like a charm. Hope this is useful to someone else.
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>';
}
Upvotes: 1
Reputation: 3470
Change
$pub_ids .= $value['pub_id'] . ';';
$titles .= '* ' . $value['title'] . '<br />';
for this:
$pub_ids = $value['pub_id'] . ';';
$titles = '* ' . $value['title'] . '<br />';
you have to remove the concatenation
Upvotes: 0