Page 1 of 1

Smarty Foreach - Make a list

Posted: Mon Feb 04, 2013 1:46 pm
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! :-)

Re: Smarty Foreach - Make a list

Posted: Mon Feb 04, 2013 7:27 pm
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

Re: Smarty Foreach - Make a list

Posted: Mon Feb 04, 2013 7:40 pm
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?

Re: Smarty Foreach - Make a list

Posted: Mon Feb 04, 2013 7:48 pm
by Dr.CSS
I can't find that module in the forge...

Re: Smarty Foreach - Make a list

Posted: Mon Feb 04, 2013 7:57 pm
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 :-)

Re: Smarty Foreach - Make a list

Posted: Mon Feb 04, 2013 10:10 pm
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;...."

Re: Smarty Foreach - Make a list

Posted: Mon Feb 04, 2013 10:59 pm
by calguy1000
Try this:

Code: Select all

{assign var='tmp' value=','|implode:$items}

Re: Smarty Foreach - Make a list

Posted: Tue Feb 05, 2013 9:09 am
by cmeilandt
Thank you psy, that was exactly what I was looking for :-)

Re: Smarty Foreach - Make a list

Posted: Thu Feb 07, 2013 9:27 pm
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}