View previous topic :: View next topic |
Author |
Message |
waqar Smarty n00b
Joined: 12 Oct 2003 Posts: 1
|
Posted: Sun Oct 12, 2003 4:23 am Post subject: in_array in smarty |
|
|
Hi guys, just wondering if we can use the in_array function in smarty to display arrays. One array will display available categories and the other array will display selected categories. The in_array will display the available categories that have not been assigned.
Thankyou for your help in advance
Danny |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Oct 12, 2003 4:47 am Post subject: |
|
|
Perhaps consider building a pre-computed array of unselected items with array_diff?
If you are looking to use in_array in a {if}, then it is easy since PHP functions are allowed as modifiers when security is not enabled (which is the default):
Code: | {if $current_item|in_array($selected_array)} |
|
|
Back to top |
|
lukas Smarty Rookie
Joined: 23 Sep 2006 Posts: 5 Location: Germany
|
Posted: Sat Sep 23, 2006 7:04 pm Post subject: |
|
|
Hi,
I just came across the exact same problem and found this thread on google, so sorry for a reply three years after the thread was started ^^.
I have the following code inside a for-each loop:
[php]{foreach from=$agegroup item=agegroup name=agegroup}
<td>
<label for="agegroup-{$smarty.foreach.agegroup.iteration}">
<input type="checkbox" name="altersklasse-{$smarty.foreach.agegroup.iteration}" value="{$agegroup.id}" id="agegroup-{$smarty.foreach.agegroup.iteration}" {if $agegroup.id|in_array($agegroupSelected)}checked{/if} /> //This is line 70
{$agegroup.kuerzel}</label>
</td>
{if $smarty.foreach.agegroup.iteration == 6} </tr><tr>{/if}
{/foreach}
[/php]
When I run this code, i get the following error message:
Quote: | Parse error: syntax error, unexpected '(' in /<ADRESS-TO-FILE>/templates_c/module^veranstaltungen^veranstaltung^%%7B^7B3^7B308EF8%%hinzufuegen.tpl.php on line 70 |
I am fairly new to smarty, what could be the reason for this error?
If i'm not mistaken, i should be allowed to put bracktes inside the {if}-Statement...or?
Would be awesome if you could help me solve this problem.
Lukas
Last edited by lukas on Tue Jan 02, 2007 7:44 pm; edited 1 time in total |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sun Sep 24, 2006 6:10 am Post subject: |
|
|
My bad.
The correct modifier syntax is:
Code: | {if $current_item|in_array:$selected_array} |
or just use the php function as usual:
Code: | {if in_array($current_item,$selected_array} |
I hate being shown up 3 years after the fact, but that's the beauty of the internet, huh?
HTH
PS: do read the relevant portions of the manual on modifiers and if statements for the details. In fact, a quick read of the manual will give you lots of good stuff to build from. |
|
Back to top |
|
deadcat Smarty Rookie
Joined: 20 Sep 2003 Posts: 10
|
Posted: Thu Dec 13, 2007 1:53 am Post subject: |
|
|
I also dont want to reply after more than one year later, but really want to say thanks!
It helps me alot |
|
Back to top |
|
vietnux Smarty n00b
Joined: 02 Apr 2011 Posts: 2
|
Posted: Tue May 31, 2011 4:35 am Post subject: |
|
|
i don't using all,
i using 'foreach' in array to search in array |
|
Back to top |
|
vietnux Smarty n00b
Joined: 02 Apr 2011 Posts: 2
|
Posted: Tue May 31, 2011 4:42 am Post subject: |
|
|
vietnux wrote: | i don't using all,
i using 'foreach' in array to search in array |
sorry all!
i used to be way is "in_array($var, $aryEvent.partner)" |
|
Back to top |
|
dgloria Smarty n00b
Joined: 10 Mar 2022 Posts: 1
|
Posted: Thu Mar 10, 2022 4:18 pm Post subject: in_array |
|
|
Code: | {if in_array($needle, $haystack)} found $needle {/if} |
|
|
Back to top |
|
|