phpsmartysmarty2

Get the previous item in Smarty


In a big shortcut, my code looks like this:

{foreach from=$raport key=thekey item=i name=itemnumber}

    <p>{$i->publicate_date|date_format:"%H:%M"}</p>

{/foreach}  

Now I would like to compare publicate dates - the current one with the previous one.

I understand that I should somehow use "-1", but no matter what I do, there's an error.

Can anyone give me a hint how to achieve it?


Solution

  • You could use the following code:

    {assign var="prev" value=false}
    
    {foreach from=$raport key=thekey item=i name=itemnumber}
    
        <p>{$i->publicate_date|date_format:"%H:%M"}</p>
    
        {if $prev neq false}
          {$prev->publicate_date} {$i->publicate_date} <-- here you can compare
        {/if} 
    
        {assign var="prev" value=$i}
    {/foreach}