Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用;循环;在smarty模板系统中?_Php_Smarty - Fatal编程技术网

Php 使用;循环;在smarty模板系统中?

Php 使用;循环;在smarty模板系统中?,php,smarty,Php,Smarty,以下是我当前的代码: {foreach from=$items item=item name=utable} {foreach from=$item.terms item=tmp name=titem} <tr class="{cycle values="odd,even"}"> REMOVED UNNECESSARY CODE FROM HERE {/foreach} {/foreach} {foreach from=$items item=item name=utable

以下是我当前的代码:

{foreach from=$items item=item name=utable}

{foreach from=$item.terms item=tmp name=titem}
<tr class="{cycle values="odd,even"}">

REMOVED UNNECESSARY CODE FROM HERE

{/foreach}
{/foreach}
{foreach from=$items item=item name=utable}
{foreach from=$item.terms item=tmp name=titem}
从这里删除了不必要的代码
{/foreach}
{/foreach}
对于
tr
类,值
奇数
偶数
正确旋转

但是,并不是每个新表都会重新开始旋转。例如,我希望每个表的第一个
tr
具有类
odd
,但是如果最后一个表以
odd
结束,那么下一个表将继续以
偶数
开始


有没有办法让它在每个表的末尾停止循环,然后在下一个表中重新开始?

您需要使用
重置
属性和
打印

PHP数据示例:

$data = [1 => [2,3,51], 2 => [5,6,1], 4 => [1,2,21]];
$smarty->assign('tables',$data);
Smarty文件示例:

<style>
    tr.odd {
        background: red;
    }
    tr.even {
        background: #fff;
    }
    table {
        margin: 50px 0;
    }
</style>


{foreach $tables as $table}
    {cycle values="" reset=true print=false}
    <table>
        {foreach $table as $row}
        <tr class="{cycle values="odd,even"}">
            <td>
                {$row}
                </td>
        {/foreach}
    </table>
{/foreach}

奇数{
背景:红色;
}
平{
背景:#fff;
}
桌子{
利润率:50px0;
}
{foreach$tables as$table}
{cycle values=”“reset=true print=false}
{foreach$表作为$row}
{$row}
{/foreach}
{/foreach}
作为替代方案,您也可以这样做:

{foreach $tables as $table}
    <table>
        {foreach $table as $row}
        {if $row@first}{assign var="reset" value=true}{/if}
        <tr class="{cycle values="odd,even" reset=$reset}">
            <td>
                {$row}
                </td>
         {assign var="reset" value=false}
        {/foreach}
    </table>
{/foreach}
{foreach$tables as$table}
{foreach$表作为$row}
{如果$row@first}{assign var=“reset”value=true}{/if}
{$row}
{assign var=“reset”value=false}
{/foreach}
{/foreach}