首页 > php框架 > Smarty 在表格里输出多列多行数据的方法

Smarty 在表格里输出多列多行数据的方法

分享一篇简单的关于Smarty 在表格里输出多列多行数据的方法,有需要的朋友可以参考一下下哦。
 代码如下 复制代码

<table border="1">

<tr>


<?section name=site loop=$siteList?>

 

<td><?$siteList[site].url?></td>

 

<?if $siteListColumn > 1 ?>

 

<?if $smarty.section.site.index != 0 && ($smarty.section.site.index + 1) % $siteListColumn == 0 ?>

</tr>

<tr>

<?/if?>

 

<?else?>

</tr>

<tr>

<?/if?>

 

<?/section?>

 

</tr>

</table>

示例中的模板定界符为 <??>

 代码如下 复制代码

<?php

 

// 说明:Smarty 在表格(table)里输出多列多行数据的方法

 

// 设置列表内容

$siteList = array(

array('url'=>'http://www.phprm.com'),

array('url'=>'http://YITU.org'),

array('url'=>'http://www.phprm.com'),

array('url'=>'http://www.baidu.com'),

array('url'=>'http://www.yahoo.com.cn'),

array('url'=>'http://www.phprm.com'),

array('url'=>'http://www.sohu.com'),

array('url'=>'http://www.sina.com.cn'),

);

 

// 列表数据统计

$siteListCount = count($siteList);

 

// 显示列数

$siteListColumn = 3;

 

// 如果列表数据不能将最后一行填满,

// 如:共 8 条记录,每列 3 条记录,最后一行还差 1 条

if($siteListColumn > 1 && $siteListCount % $siteListColumn != 0)

{

// 计算最后一行还差几列

$emptyColumn = $siteListColumn - ($siteListCount % $siteListColumn);

 

for($i=0; $i<$emptyColumn; $i++)

{

// 用空值填充,保证最后一行正确显示

$siteList[$siteListCount + $i] = array('url'=>'&nbsp;');

}

}

 

// 将显示列数赋值到模板

$smarty->assign('siteListColumn', $siteListColumn);

 

// 将显示数据赋值到模板

$smarty->assign('siteList', $siteList);

 

?>



本文地址:http://www.phprm.com/frame/40894.html

转载随意,但请附上文章地址:-)

标签:none

发表留言