Define new variable created by FOREACH
7 posts by 3 authors in: Forums > CMS Builder
Last Post: April 1, 2009 (RSS)
By rjbathgate - March 31, 2009
I have a multi record table, which includes the field 'email_address', and have a display page showing this:
<?php foreach ($contactsRecords as $record): ?>
<?php echo $record['email_address'] ?>
<?php endforeach; ?>
I need to be able to assign the output to a new variable, such as:
$allemails = email@email.com, email2@email.com, email3@email.com
So I can then on a subsequent page display:
"email@email.com, email2@email.com, email3@email.com".
I need to assign it to the new varaible ($allemails) so it can indeed be displayed/posted into a subsequent page.
The code
<?php $recipients = '<?php foreach ($contactsRecords as $record): ?><?php echo $record['email_address'] ?><?php endforeach; ?>' ?>
Is the principle, but obviously doesn't work as I'm trying to put <?php> tags within already open <?php> tags.
I've tried it cutting out the second set of tags, but still doesn't work.
So i'm not sure if it's just a slight error with my line, or i'm barking up the wrong tree...
I touched in the Concatenation idea, but not sure how to make it concatenate FOREACH.
Any ideas/thoughts would be most appreciated.
Many thanks
Rob
Re: [rjbathgate] Define new variable created by FOREACH
By Dave - March 31, 2009
Try this:
$recipients = '';
foreach ($contactsRecords as $record) {
$recipients .= $record['email_address'] . ', ';
}
$recipients = rtirm($recipients, ', '); // remove trailing comma and space
Let me know if that works for you.
interactivetools.com
Re: [Dave] Define new variable created by FOREACH
By rjbathgate - March 31, 2009
Cheers for that... I receive the error:
Fatal error: Call to undefined function rtirm() in /home/hartford/public_html/cmsAdmin/lib/menus/search-results.php on line 136
Line 136 is:
$recipients = rtirm($recipients, ', '); // remove trailing comma and space
Any ideas?
Cheers
Rob
Re: [rjbathgate] Define new variable created by FOREACH
By rjbathgate - March 31, 2009
<?php
$allemails = '';
$firstEntry = true;
foreach($contactsRecords as $record){
if(!$firstEntry){
$allemails .= ', ';
}
else{
$firstEntry = false;
}
$allemails .= $record['email_address_1'];
}
?>
Note, variable in that code is $allemails as oppose to $recipients, but hey, same thing...
Re: [rjbathgate] Define new variable created by FOREACH
By ross - April 1, 2009
Just wanted to make sure that last post meant you got things going :). Let me know if you still need a hand.
Thanks!
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com
Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/
Re: [rjbathgate] Define new variable created by FOREACH
By Dave - April 1, 2009
Sorry about the typo, I meant to type rtrim(). Here's the docs for it for next time so you can have one more tool in your toolbox: http://www.php.net/rtrim
Hope that helps!
interactivetools.com
Re: [Dave] Define new variable created by FOREACH
By rjbathgate - April 1, 2009
Yeah got it working cheers...
I didn't even notice the type rtirm -- knew it was rtrim, I even read it as rtrim!!
Cheers all
Rob