如何使用Smarty打印记录集?

发布于 2024-12-09 18:58:34 字数 363 浏览 0 评论 0原文

我需要显示查询(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}

我该如何优化它?

I need to show the result of a query(mysql), how can I loop the recordset without assign the values to an array?
Now I do:

while($row = $this->mysql->fetch($rs)){
  val[] = $row
}
$this->smarty->assign('val', val);

then (in the template.tpl)

{section name=nr loop=$val}
{$val[nr].cod}<br />
{sectionelse}
<h1>No record</h1>
{/section}

How can I optimize it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-12-16 18:58:34

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

{if $val}
    {foreach from=$val item=nr}
        {$nr.cod}<br />
    {/foreach}
{else}
    <h1>No record</h1>
{/if}

You can use Smarty's foreach, although it doesn't make it any shorter:

{if $val}
    {foreach from=$val item=nr}
        {$nr.cod}<br />
    {/foreach}
{else}
    <h1>No record</h1>
{/if}
赤濁 2024-12-16 18:58:34

也许 {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})

您还可以输出单元格的键:

{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}

maybe {html_table} can help. Otherwise you're free to iterate your rows and cols however you like:

{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}

(Smarty3 syntax of {foreach})

You can also output the cells' keys:

{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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文