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

Operations with array indexes

 
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
c0p0n
Smarty n00b


Joined: 15 Jul 2003
Posts: 4

PostPosted: Tue Sep 30, 2003 2:27 pm    Post subject: Operations with array indexes Reply with quote

I wanna do this:
Code:
{section name=i loop=$datos}
         {if ($datos[i].idmedia != $datos[i-1].idmedia)}
            <b>CD: {$datos[i].idmedia}</b>
         {/if}
         <p>
         {$datos[i].path}/{$datos[i].name}
         </p>
      {/section}

The problem is at $datos[i-1].idmedia stuff, it seems that I cannot do it. I've tried also:
Code:
{section name=i loop=$datos}
         {assign var="j" value="$i-1"}
         {if ($datos[i].idmedia != $datos[j].idmedia)}
            <b>CD: {$datos[i].idmedia}</b>
         {/if}
         <p>
         {$datos[i].path}/{$datos[i].name}
         </p>
      {/section}

With no luck.

The thing that I want to do is simple: if the content of that var is not equal to the content of the same var, but on the previous loop, the template should show a text.

(the source is a list of archives, ordered by CD-ROM numbers. If the actual CD is not the same as the previous, it begins a new CD-ROM files listing so it should display the actual CD).

Edit: oh! I forgot!. Smarty 2.5.0
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Sep 30, 2003 3:49 pm    Post subject: Re: Operations with array indexes Reply with quote

maybe sth. like this:

(untested)
Code:
{assign var=recent_id value=-1} {* some "illegal" id *}
{section name=i loop=$datos}
   {if ($datos[i].idmedia != recent_id)}
      <b>CD: {$datos[i].idmedia}</b>
      {assign var=recent_id value=$datos[i].idmedia}
   {/if}
   <p>
   {$datos[i].path}/{$datos[i].name}
   </p>
{/section}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
c0p0n
Smarty n00b


Joined: 15 Jul 2003
Posts: 4

PostPosted: Tue Sep 30, 2003 4:12 pm    Post subject: Reply with quote

thank U sir! it worked, with a little modification:
Code:
{assign var=recent_id value=-1}
      {section name=i loop=$datos}
         
         {if ($datos[i].idmedia != $recent_id)}
            <b>CD: {$datos[i].idmedia}</b>
            {assign var=recent_id value=$datos[i].idmedia}
         {/if}
         <p>
         {$datos[i].path}/{$datos[i].name}
         </p>
      {/section}

I had to put the first {assign} statement before the loop, because it reseted to -1 every time the loop went back to beginning (I think that that was the way you thought it, but you wrote it badly).
again, thanx! ;D
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Sep 30, 2003 4:51 pm    Post subject: Reply with quote

what are you smokin'? of course i initilalized recent_id *outside* of the section.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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