Repeated in the table Smarty + Php

假如想象 提交于 2019-12-25 03:36:40

问题


I have a problem in in the table

The problem is to repeat I want when it reaches 4 rows to the table is transferred to the new line

Code PHP :

    // for : 
$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);
} 

Code Html smarty :

<td width="91"><table width="100" height="100" border="0" cellpadding="1" cellspacing="1" bgcolor="#666666">
<tbody><tr>
    {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} 
</tr>

回答1:


First of all you are reassigning tr in every iteration, and fetching template it outside while loop, so it makes no sense. You should assign variable after fetching all results:

while($row = mysql_fetch_array($post_tv)){ 
    $show[] = $row; 
}
$marsosmarty->assign("show", $show);

To move to the next row in table, you can use section name and modulo operator like this:

<td width="91"><table width="100" height="100" border="0" cellpadding="1" cellspacing="1" bgcolor="#666666">
<tbody><tr>
    {section name=table loop=$show}
    <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>
    {if !$smart.section.table.last && $smart.section.table.iteration % 4 eq 0}
         </tr><tr>
    {/if}
    {/section}
</tr>

This way, after displaying 4 cells new table row is created (only if there are more cells, thats ensured by this !$smart.section.table.last condition)



来源:https://stackoverflow.com/questions/8779050/repeated-in-the-table-smarty-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!