Php 如何使用Smarty打印记录集?

Php 如何使用Smarty打印记录集?,php,smarty,Php,Smarty,我需要显示一个查询的结果(mysql),如何在不将值分配给数组的情况下循环记录集? 现在我做到了: while($row = $this->mysql->fetch($rs)){ val[] = $row } $this->smarty->assign('val', val); 然后(在template.tpl中) {section name=nr loop=$val} {$val[nr].cod} {sectionelse} 没有记录 {/section} 如何

我需要显示一个查询的结果(mysql),如何在不将值分配给数组的情况下循环记录集? 现在我做到了:

while($row = $this->mysql->fetch($rs)){
  val[] = $row
}
$this->smarty->assign('val', val);
然后(在template.tpl中)

{section name=nr loop=$val}
{$val[nr].cod}
{sectionelse} 没有记录 {/section}

如何优化它?

您可以使用Smarty的foreach,尽管它不会使它变得更短:

{if $val}
    {foreach from=$val item=nr}
        {$nr.cod}<br />
    {/foreach}
{else}
    <h1>No record</h1>
{/if}
{if$val}
{foreach from=$val item=nr}
{$nr.cod}
{/foreach} {else} 没有记录 {/if}
您可以使用Smarty的foreach,尽管它不会使它变得更短:

{if $val}
    {foreach from=$val item=nr}
        {$nr.cod}<br />
    {/foreach}
{else}
    <h1>No record</h1>
{/if}
{if$val}
{foreach from=$val item=nr}
{$nr.cod}
{/foreach} {else} 没有记录 {/if}
也许能帮上忙。否则,您可以随意迭代行和列:

{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$val作为$row}
{如果$row@first}
{/if}
{foreach$行作为$cell}
{如果$cell@first}
{/if}
{$cell | escape:“html}
{如果$cell@last}
{/if}
{foreach}
{如果$row@last}
{/if}
{foreachelse}
没有数据

{/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}
{foreach$val作为$row}
{如果$row@first}
{foreach$行作为$cell}
{$cell@key|转义:“html}
{/foreach}
{/if}
{foreach$行作为$cell}
{如果$cell@first}
{/if}
{$cell | escape:“html}
{如果$cell@last}
{/if}
{foreach}
{如果$row@last}
{/if}
{foreachelse}
没有数据

{/foreach}
可能会有所帮助。否则,您可以随意迭代行和列:

{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$val作为$row}
{如果$row@first}
{/if}
{foreach$行作为$cell}
{如果$cell@first}
{/if}
{$cell | escape:“html}
{如果$cell@last}
{/if}
{foreach}
{如果$row@last}
{/if}
{foreachelse}
没有数据

{/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}
{foreach$val作为$row}
{如果$row@first}
{foreach$行作为$cell}
{$cell@key|转义:“html}
{/foreach}
{/if}
{foreach$行作为$cell}
{如果$cell@first}
{/if}
{$cell | escape:“html}
{如果$cell@last}
{/if}
{foreach}
{如果$row@last}
{/if}
{foreachelse}
没有数据

{/foreach}
否,我询问如何显示“记录集”(我的记录集),正如您所看到的,我将行保存在数组中,是否需要这样做?我不能直接打印记录集吗?@Dail除非在Smarty模板中嵌入PHP代码,否则在不将记录集保存到数组的情况下无法打印记录集。这是一种不可取的做法。首先将其存储到数组是正确的处理方法。如果您使用PDO(或提供可迭代结果集的任何DB抽象),则可以分配结果集,并让smarty为您迭代。(这不适用于旧的mysql_query()内容)不,我问如何显示“记录集”(我的记录集),正如您看到的,我将行保存在数组中,我需要这样做吗?我不能直接打印记录集吗?@Dail除非在Smarty模板中嵌入PHP代码,否则在不将记录集保存到数组的情况下无法打印记录集。这是一种不可取的做法。首先将其存储到数组是正确的处理方法。如果您使用PDO(或提供可迭代结果集的任何DB抽象),则可以分配结果集,并让smarty为您迭代。(这不适用于旧的mysql_query()内容)