Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Problem with if and array

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
timoraes
Smarty Rookie


Joined: 12 Apr 2016
Posts: 6

PostPosted: Tue Apr 12, 2016 11:02 am    Post subject: Problem with if and array Reply with quote

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
View user's profile Send private message
timoraes
Smarty Rookie


Joined: 12 Apr 2016
Posts: 6

PostPosted: Wed Apr 13, 2016 4:48 pm    Post subject: Reply with quote

Any ideas??
Back to top
View user's profile Send private message
prestondocks
Smarty n00b


Joined: 13 Apr 2016
Posts: 4

PostPosted: Wed Apr 13, 2016 7:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
timoraes
Smarty Rookie


Joined: 12 Apr 2016
Posts: 6

PostPosted: Wed Apr 13, 2016 7:17 pm    Post subject: Reply with quote

I've tried this, doesn't work. I'm using smarty 3.

Thanks for reply.
Back to top
View user's profile Send private message
prestondocks
Smarty n00b


Joined: 13 Apr 2016
Posts: 4

PostPosted: Wed Apr 13, 2016 7:49 pm    Post subject: Reply with quote

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
View user's profile Send private message
timoraes
Smarty Rookie


Joined: 12 Apr 2016
Posts: 6

PostPosted: Wed Apr 13, 2016 8:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
prestondocks
Smarty n00b


Joined: 13 Apr 2016
Posts: 4

PostPosted: Wed Apr 13, 2016 8:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
timoraes
Smarty Rookie


Joined: 12 Apr 2016
Posts: 6

PostPosted: Wed Apr 13, 2016 8:40 pm    Post subject: Reply with quote

I'm pretty noob on smarty Confused ...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 Sad
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Apr 19, 2016 11:56 pm    Post subject: Reply with quote

$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
View user's profile Send private message
timoraes
Smarty Rookie


Joined: 12 Apr 2016
Posts: 6

PostPosted: Wed Apr 20, 2016 2:25 am    Post subject: Reply with quote

Thanks, but this way returns 0.00 to lastpart and 0 to lastvalue.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP