View previous topic :: View next topic |
Author |
Message |
timoraes Smarty Rookie
Joined: 12 Apr 2016 Posts: 6
|
Posted: Tue Apr 12, 2016 11:02 am Post subject: Problem with if and array |
|
|
Hi, i need to show only the last item of a list (array) before the value 150. I've tried something like this, but no results:
Code: | {$parts = ['2','3','4','5','6','7','8','9','10']}
{foreach $parts as $value name=parts}
{$part = $total/$value}
{if $part>=150}
{if $smarty.foreach.parts.last}
{$value}x {$part}
{/if}
{/if}
{/foreach} |
Thanks in advance![/code] |
|
Back to top |
|
timoraes Smarty Rookie
Joined: 12 Apr 2016 Posts: 6
|
Posted: Wed Apr 13, 2016 4:48 pm Post subject: |
|
|
Any ideas?? |
|
Back to top |
|
prestondocks Smarty n00b
Joined: 13 Apr 2016 Posts: 4
|
Posted: Wed Apr 13, 2016 7:10 pm Post subject: |
|
|
Try adding your second if as a and inside the first if. I am new to smarty, but I think I have read in the documentation that you can do that.
example
Code: | {$parts = ['2','3','4','5','6','7','8','9','10']}
{foreach $parts as $value name=parts}
{$part = $total/$value}
{if $part>=150 && $smarty.foreach.parts.last}
{$value}x {$part}
{/if}
{/foreach} |
Also are you using smarty 2 or smarty 3. Smarty 3 would be slightly different. |
|
Back to top |
|
timoraes Smarty Rookie
Joined: 12 Apr 2016 Posts: 6
|
Posted: Wed Apr 13, 2016 7:17 pm Post subject: |
|
|
I've tried this, doesn't work. I'm using smarty 3.
Thanks for reply. |
|
Back to top |
|
prestondocks Smarty n00b
Joined: 13 Apr 2016 Posts: 4
|
Posted: Wed Apr 13, 2016 7:49 pm Post subject: |
|
|
if your using Smarty 3, then you should be able to use
Code: |
{foreach $parts as $value}
{if $value@last}
{/if}
{/foreach}
|
|
|
Back to top |
|
timoraes Smarty Rookie
Joined: 12 Apr 2016 Posts: 6
|
Posted: Wed Apr 13, 2016 8:03 pm Post subject: |
|
|
Do you mean this?
Code: | {$parts = ['2','3','4','5','6','7','8','9','10']}
{foreach $parts as $value name=parts}
{$part = $total/$value}
{if $part>=150}
{if $value@last}
{$value}x {$part}
{/if}
{/if}
{/foreach} |
|
|
Back to top |
|
prestondocks Smarty n00b
Joined: 13 Apr 2016 Posts: 4
|
Posted: Wed Apr 13, 2016 8:27 pm Post subject: |
|
|
Yep, but I don't know why you are needing to re reference $value to parts. I don't think you need to do that anymore in smarty 3 |
|
Back to top |
|
timoraes Smarty Rookie
Joined: 12 Apr 2016 Posts: 6
|
Posted: Wed Apr 13, 2016 8:40 pm Post subject: |
|
|
I'm pretty noob on smarty ...well, I tried this way:
Code: |
{$parts = ['2','3','4','5','6','7','8','9','10']}
{foreach $parts as $value}
{$part = $total/$value}
{if $part>=150}
{if $value@last}
{$value}x {$part}
{/if}
{/if}
{/foreach} |
same wrong results |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Apr 19, 2016 11:56 pm Post subject: |
|
|
$smarty.foreach.parts.last or $value@last is true when the loop is executed for the last array element. It does not help you.
Code: |
{$parts = ['2','3','4','5','6','7','8','9','10']}
{$lastpart = 0}{$lastvalue = 0}
{foreach $parts as $value}
{$part = $total/$value}
{if $part>=150}
{$lastvalue}x {$lastpart}
{break}
{else}
{$lastpart = $part}{$lastvalue = $value}
{/if}
{/foreach} |
|
|
Back to top |
|
timoraes Smarty Rookie
Joined: 12 Apr 2016 Posts: 6
|
Posted: Wed Apr 20, 2016 2:25 am Post subject: |
|
|
Thanks, but this way returns 0.00 to lastpart and 0 to lastvalue. |
|
Back to top |
|
|