开发者

How to limit the number of cells per table row to 3?

I have a dynamically filled table, in which I would like to limit the number of columns to 3, so that the next td stars on the next line. (See http://kbay.in and check the categories button at the top开发者_运维知识库 of the page.)

Here's how I import the data using Smarty PHP:

<table cellpadding="0" cellspacing="4" width="100"; border-collapse="collapse";>
<tr>

{foreach from=$array_categories item=v name=cat}
<td valign="top" width="116px" align="left">
{capture name=some_content assign=categ_url}
{if $v.subcats>0}{$live_site}/{if $seo_settings.enable_mod_rewrite}{$v.id}-{$v.url_title}/1/listings.html{else}listings.php?category={$v.id}{/if}{/if}
{/capture}

                <a href="{$categ_url}">{$v.name|escape:"html"}{if $appearance.categ_count_ads} ({$v.ads}) {/if}</a>  


{/foreach}</td></tr> </table>

How can I make it so that there are only 3 table cells in each row?


Use the iteration property of foreach loop:

...
{foreach from=$array_categories item=v name=cat}
{if $smarty.foreach.cat.iteration % 3 == 0 && $smarty.foreach.cat.iteration > 0}
</tr>
<tr>
{/if}
<td valign="top" width="116px" align="left">
...

$smarty.foreach.cat.iteration is Smarty2 syntax, if you use Smarty3 you can also use $v@iteration.


increment a counter each time in the loop and do a check within your loop starting a new row when necessary. Something like:

if ($counter % 3 == 0) {
    echo '</tr><tr>';
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜