我需要显示查询结果(mysql),如何在不将值分配给数组的情况下循环记录集? 现在我做:
while($row = $this->mysql->fetch($rs)){
val[] = $row
}
$this->smarty->assign('val', val);
然后(在template.tpl中)
{section name=nr loop=$val}
{$val[nr].cod}<br />
{sectionelse}
<h1>No record</h1>
{/section}
我该如何优化它?
答案 0 :(得分:1)
你可以使用Smarty的foreach,虽然它不会缩短它:
{if $val}
{foreach from=$val item=nr}
{$nr.cod}<br />
{/foreach}
{else}
<h1>No record</h1>
{/if}
答案 1 :(得分:0)
也许{html_table}可以提供帮助。否则,您可以随意迭代您的行和列:
{foreach $val as $row}
{if $row@first}
<table>
<tbody>
{/if}
{foreach $row as $cell}
{if $cell@first}
<tr>
{/if}
<td>{$cell|escape:"html}</td>
{if $cell@last}
</tr>
{/if}
{foreach}
{if $row@last}
</tbody>
</table>
{/if}
{foreachelse}
<p>No Data</p>
{/foreach}
({foreach}的Smarty3语法)
您还可以输出单元格的键:
{foreach $val as $row}
{if $row@first}
<table>
<thead>
<tr>
{foreach $row as $cell}
<th>{$cell@key|escape:"html}</th>
{/foreach}
</tr>
</thead>
<tbody>
{/if}
{foreach $row as $cell}
{if $cell@first}
<tr>
{/if}
<td>{$cell|escape:"html}</td>
{if $cell@last}
</tr>
{/if}
{foreach}
{if $row@last}
</tbody>
</table>
{/if}
{foreachelse}
<p>No Data</p>
{/foreach}