Smarty Foreach - Make a list

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
cmeilandt
New Member
New Member
Posts: 7
Joined: Mon Feb 04, 2013 10:54 am

Smarty Foreach - Make a list

Post by cmeilandt »

Hi!
I'm new and I've tried to search around the web to learn how to do this in smarty. I want to get all the Emails to be wrote out in one long variable in smarty.

My Smarty Foreach:

Code: Select all

{foreach from=$items item=entry}
All the values for every item.....
{/foreach}
In PHP I would do like this:

Code: Select all

$AllEmail = $AllEmail;
while($row = mysql_fetch_array($result))
{
$AllEmail = $AllEmail . $row['email'].",";
};
Thanks for helping a newbie a this smarty thing! :-)
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: Smarty Foreach - Make a list

Post by Dr.CSS »

Sort of lack enuf info to really help, what email from where etc...

Please read this...

http://forum.cmsmadesimple.org/viewtopi ... =40&t=2661
cmeilandt
New Member
New Member
Posts: 7
Joined: Mon Feb 04, 2013 10:54 am

Re: Smarty Foreach - Make a list

Post by cmeilandt »

Okay, I'll try to explain me a little better :-):

I use FrontEndUser Listning 0.4-beta-3. I show all the users, with names and email, in a given group in a tabel. After that, I want to make a "Email All users" button. So I was thinking of make a list trough the foreach: email1, email2, email3,... etc. to use for that purpose. :-)

Sorry that I didn't explained that the first time. Make sense?
User avatar
Dr.CSS
Moderator
Moderator
Posts: 12711
Joined: Thu Mar 09, 2006 5:32 am

Re: Smarty Foreach - Make a list

Post by Dr.CSS »

I can't find that module in the forge...
cmeilandt
New Member
New Member
Posts: 7
Joined: Mon Feb 04, 2013 10:54 am

Re: Smarty Foreach - Make a list

Post by cmeilandt »

Okay, the thing I really needs help for, is understanding the syntax of smarty.
In PHP I could do something like this in a loop:

Code: Select all

$allEmail = $allEmail .' + ' . $row['email'];
When the loop is done, I can do echo $allEmail and I'll get a long list of all emails together in one variable :-)
psy
Power Poster
Power Poster
Posts: 463
Joined: Sat Jan 22, 2005 11:19 am

Re: Smarty Foreach - Make a list

Post by psy »

See http://www.smarty.net/docs/en/language. ... oreach.tpl and http://www.smarty.net/docs/en/language.modifier.cat.tpl

Code: Select all

{assign var='allemails' value=''}
{foreach from=$yourfulllistofitems item='item'}
  {assign var='allemails' value=$allemails|cat:$item.email|cat:';'}
{/foreach}
This will create a var, $allemails, which is a string comprising all the email addresses separated by semi-colons, eg:

"one@email1.com;one@email2.com;one@email3.com;...."
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Smarty Foreach - Make a list

Post by calguy1000 »

Try this:

Code: Select all

{assign var='tmp' value=','|implode:$items}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
cmeilandt
New Member
New Member
Posts: 7
Joined: Mon Feb 04, 2013 10:54 am

Re: Smarty Foreach - Make a list

Post by cmeilandt »

Thank you psy, that was exactly what I was looking for :-)
psy
Power Poster
Power Poster
Posts: 463
Joined: Sat Jan 22, 2005 11:19 am

Re: Smarty Foreach - Make a list

Post by psy »

Here is a way to create a new array rather than a string.

Note that you do not have to create the var first. If it doesn't exist, Smarty will create it on the first iteration.

Code: Select all

{foreach from=$myfulllist item='item'}
 {append var='allemails' value=$item.email|cat:';'}
{/foreach}
Post Reply

Return to “The Lounge”