【问题标题】:Repeated table Smarty template重复表 Smarty 模板
【发布时间】:2012-01-08 19:55:41
【问题描述】:
我的表格有问题
问题是重复我想要的当它达到4行时将表转移到新行
PHP代码:
$tr = 1;
while($row = mysql_fetch_array($post_tv)){
$show[] = $row;
if ($tr == 4){
$tr == 1;
}
$tr++;
$marsosmarty->assign("show",$show);
$marsosmarty->assign("tr",$tr);
}
代码 Html 聪明:
{section name=table loop=$show}
{if $tr eq 3} </tr><tr> {/if}
<td bgcolor="#FFFFFF">
<a href="./channel.php?id={$show[table].id}" target="az">
<img src="{$show[table].a_IMG}" alt="{$show[table].a_DESC}" width="100" height="100" border="0" class="link-img" title="{$show[table].a_TITLE}">
</a>
</td>
{/section}
【问题讨论】:
标签:
php
html
templates
while-loop
smarty
【解决方案1】:
试试这个:
$tr = 1;
while($row = mysql_fetch_array($post_tv)){
$show[] = $row;
if ($tr == 4){
$tr = 1; // You have to use '=' instead of '=='
}
$tr++;
$marsosmarty->assign("show",$show);
$marsosmarty->assign("tr",$tr);
}
记住'=='是比较变量,它返回TRUE或FALSE。相反,'=' 是将变量设置为特定值。
【解决方案2】:
你做错了。
你必须在 PHP 中正常填充数组,不要关心你 PHP 代码中的表:
while($row = mysql_fetch_array($post_tv)){
$show[] = $row;
}
$marsosmarty->assign("show",$show);
那么在显示数据时,每次当前迭代索引为四的倍数(4、8、12、...)时,都必须打印一个</tr><tr>。您可以使用模数运算符来完成此操作(Smarty 中的mod,PHP 中的%,请参阅here)。所以,你需要这样的东西:
{section name=table loop=$show}
{if ($smarty.section.table.index mod 4 == 0) && ($smarty.section.table.index != 0)} </tr><tr> {/if}
<td bgcolor="#FFFFFF">
<a href="./channel.php?id={$show[table].id}" target="az">
<img src="{$show[table].a_IMG}" alt="{$show[table].a_DESC}" width="100" height="100" border="0" class="link-img" title="{$show[table].a_TITLE}">
</a>
</td>
{/section}
我使用了$smarty.section.table.index 特殊变量来告诉您当前的迭代索引(请参阅here)。第一个条件需要搜索四的倍数,第二个条件是避免在第一次迭代时打印行尾。
让我知道它是否有效,我已经编写了代码而没有测试它。