View previous topic :: View next topic |
Author |
Message |
fLowww Smarty Rookie
Joined: 30 Sep 2003 Posts: 7
|
Posted: Tue Sep 30, 2003 2:49 pm Post subject: Problem with mod in connection with `if` |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Sep 30, 2003 3:02 pm Post subject: |
|
|
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 |
|
fLowww Smarty Rookie
Joined: 30 Sep 2003 Posts: 7
|
Posted: Tue Sep 30, 2003 3:25 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Sep 30, 2003 4:05 pm Post subject: |
|
|
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 |
|
fLowww Smarty Rookie
Joined: 30 Sep 2003 Posts: 7
|
Posted: Tue Sep 30, 2003 4:16 pm Post subject: |
|
|
I have difficulties to understand why it is like this but it seems to be true
why $i+1 ?
greetz
fLo
and already thanks in advance ^^ |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Sep 30, 2003 4:53 pm Post subject: |
|
|
{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 |
|
fLowww Smarty Rookie
Joined: 30 Sep 2003 Posts: 7
|
Posted: Tue Sep 30, 2003 6:12 pm Post subject: |
|
|
Great Thanx
greetz
fLo |
|
Back to top |
|
|