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 mod in connection with `if`

 
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
fLowww
Smarty Rookie


Joined: 30 Sep 2003
Posts: 7

PostPosted: Tue Sep 30, 2003 2:49 pm    Post subject: Problem with mod in connection with `if` Reply with quote

Hi smarty-friends

I want to have to have a couple of news in a row.
In PHP I would manage it like this :

[php:1:34531e6422]<?php
echo "<tr>";

for($i = 1; $i <= 10; $i++)
{
echo "<td>".$news[$i]."</td>";

if(($i % 2 == 0)&&($i != 10))
{
echo "</tr><tr>";
}
ecoh "</tr>";
?>[/php:1:34531e6422]
But I want to have this done in my template so I found this:

########## not relevant , skip! ################

{* Monte Ohrt March 4, 2002 *}
{* $cols is the number of columns you want *}
<TABLE border=1>
<TR>
{section name=numloop loop=$data}
<TD>{$data[numloop]}</TD>
{* see if we should go to the next row *}
{if not ($smarty.section.numloop.rownum mod $cols)}
{if not $smarty.section.numloop.last}
</TR><TR>
{/if}
{/if}
{if $smarty.section.numloop.last}
{* pad the cells not yet created *}
{math equation = "n - a % n" n=$cols a=$data|@count assign="cells"}
{if $cells ne $cols}
{section name=pad loop=$cells}
<TD>&</TD>
{/section}
{/if}
</TR>
{/if}
{/section}
</TABLE>

#######################################

The Problem lies in this part of the code :

{if not ($smarty.section.numloop.rownum mod $cols)}

I've searched the manual for further informations about mod but I dind't find anything, I know that mod is the same (?) as % in PHP but It doesn't make any sense to me.

Any help and idea is appreciated.


greetz
fLo[/b]
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:02 pm    Post subject: Reply with quote

what does not make sense to you?

the fact that "mod" is alias for "%" (just like "and" is an alias for "&&" (somehow)) ???

or the expression:
{if not ($smarty.section.numloop.rownum mod $cols)}

which would translate to
if (! (($i+1) % $cols)) { ...
which is the same as
if ((($i+1) % $cols) != 0) { ...

???
Back to top
View user's profile Send private message Send e-mail Visit poster's website
fLowww
Smarty Rookie


Joined: 30 Sep 2003
Posts: 7

PostPosted: Tue Sep 30, 2003 3:25 pm    Post subject: Reply with quote

Thanks for you quick answer but I still doesn't understand it.

This Codes says that every time `rownum` is divisible through `cols` there should be a new line.

{if not ($smarty.section.numloop.rownum mod $cols)}
</TR><TR>
{/if}

########
In My eyes it must be :
{if $smarty.section.numloop.rownum mod $cols == 0}
otherwise it makes no sense

greetz fLo
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:05 pm    Post subject: Reply with quote

sorry, my fault, of course it should read:

"if (! (($i+1) % $cols)) { ...
is merely the same as
if ((($i+1) % $cols) == 0) { ... "

that means

{if not ($smarty.section.numloop.rownum mod $cols)}
behaves just like
{if $smarty.section.numloop.rownum mod $cols == 0}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
fLowww
Smarty Rookie


Joined: 30 Sep 2003
Posts: 7

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

I have difficulties to understand why it is like this Wink but it seems to be true

why $i+1 ?

greetz
fLo

and already thanks in advance ^^
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:53 pm    Post subject: Reply with quote

{section name=numloop loop=$data}

our [numloop] (which is $i) runs from 0 to (sizeof($data)-1)
our $smarty.section.numloop.rownum runs from 1 to sizeof($data)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
fLowww
Smarty Rookie


Joined: 30 Sep 2003
Posts: 7

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

Great Thanx Wink

greetz
fLo
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