1

how to get last 7 element of php array in smarty ? I wrote code to get last element

      {foreach from=$results item=result name=forsmart}
                                        {if $smarty.foreach.forsmart.last}
                                           {$result->getAvgtimeonpage()|date_format:"%M :%S"}
                                        {/if}
                                    {/foreach}

how to get last 7 elements ?

Best Regards, thanks for help

woj_jas
  • 1,092
  • 7
  • 23
  • 50

3 Answers3

3

In regards to MarkS answer, you don't have to assign a counter. Smarty has a built-in total.

{foreach from=$results item=result name=forsmart}
       {if $smarty.foreach.forsmart.iteration > ($smarty.foreach.forsmart.total - 7)}
             {$result->getAvgtimeonpage()|date_format:"%M :%S"}
       {/if}
{/foreach}
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • Good point for foreach loop, still I'd rather go with for loop, because you dont need to iterate through whole result array. Of course it will rather affect time on huge array. – MSadura Feb 17 '14 at 14:34
  • @Panama Jack That's not working , results empty string – woj_jas Feb 17 '14 at 14:38
  • @woj_jas Sorry misspelled iteration. It should work now. – Panama Jack Feb 17 '14 at 14:47
  • @PanamaJack now it's working , Can you please tell me how to sum all elements in your code ? Thanks for answer, regards – woj_jas Feb 17 '14 at 14:51
1

In your foreach:

1) Assignt counter:

{assign var=toShow value=(($results|@count) - 7)}

2) Show items with index greater than counter:

 {foreach from=$results item=result name=forsmart}
       {if $smarty.foreach.forsmart > $toShow}
             {$result->getAvgtimeonpage()|date_format:"%M :%S"}
       {/if}
{/foreach}

In for loop:

1) Assign loop delimiters

{assign var=maxCount value=(($results|@count)}
{assign var=toShow value=($maxCount - 6)}

2) Loop 'n roll:

{for $i=$toShow to $maxCount}
    {$results[i]->getAvgtimeonpage()|date_format:"%M :%S"}
{/for}

Sorry if I've made any misspeling, but I hope you will get the idea.

MSadura
  • 1,032
  • 13
  • 18
  • Thanks, now it's taking last 7 elements. Can you tell me how to sum them ? I used that code from 1) and 2) in foreach. and I've got 7 time items. I want to sum them in 1 time item – woj_jas Feb 17 '14 at 13:55
  • If you want to get sum, assign new variable i.e `{assign var=timeTotal value=0}` (before loop) and change loop line to : `{assign var=timeTotal value=$timeTotal+ ($results[i]->getAvgtimeonpage()|date_format:"%M :%S")}` and after looping you can show value by {$timeTotal} . HOWEVER, I dont know your code but I suggest you to do that loop and sum in php and just pass calculated total time to smarty. – MSadura Feb 17 '14 at 14:47
  • I use Google maps api and it's almost impossible to do something in php without errors so i'm trying not to do anything ;) – woj_jas Feb 17 '14 at 14:50
  • I'm not a google map specialist so cant help you with that. Even if doing that calculation in smarty is not elegant, it should work. – MSadura Feb 17 '14 at 15:01
0

You can try this

$arrayLength = count($results);
for ($i = $arrayLength-7; $i < $arrayLength; $i++) {
    ...
}

I edit my post to provide code for smarty

{assign arrayLength = {$results|@count}}
{for $i=$arrayLength-1 to $arrayLength}
    ...
{/for}
miltos
  • 1,009
  • 1
  • 6
  • 10