【问题标题】:Smarty: Same row getting displayed more than onceSmarty:同一行显示不止一次
【发布时间】:2015-06-03 07:39:15
【问题描述】:

这是我的 PHP 代码(返回具有 2 列的行的查询:ReportId 和 MailingFrequency):

while($x=mysql_fetch_assoc($result1))
    {
    $X[0] = $x['ReportId'];
    $X[1] = $x['MailingFrequency'];
    $X[2] = 'Stop Subscription';
    $this->smarty->assign("X", $X);
    }

我的 HTML 代码(显示 smarty 变量 X 的数据):

{section name=index loop = $X}   
          <tr> 
          <td width="25%">{$X[0]}</td>
          <td width="35%">{$X[1]}</td>
          <td width="40%"><a href="../webadmin/stopsubscription" id="StopSubscription" style="color: #CC0000">{$X[2]}</a></td>
      </tr>

现在,如果数据集只有一行格式(ReportId,MailingFrequency),例如(1,'Saturday'),它可以正确显示。但是,在多行的情况下,仅显示最后一行。 如何显示html表格中的所有行?

【问题讨论】:

    标签: php html mysql html-table smarty


    【解决方案1】:

    在您的代码中,X 值不断被覆盖:

    $list = array();
    while($x=mysql_fetch_assoc($result1))
    {
        $list[] = array(
            'report_id' => $x['ReportId'],
            'mailing frequency' => $x['MailingFrequency'],
            'text' => 'Stop Subscription'
        );
    }
    $this->smarty->assign(compact('list'));
    

    查看:

    {foreach from=list item=val}   
      <tr> 
          <td width="25%">{$val.report_id}</td>
          <td width="35%">{$val.mailing_frequency}</td>
          <td width="40%"><a href="../webadmin/stopsubscription" id="StopSubscription" style="color: #CC0000">{$val.text}</a></td>
      </tr>
    {/foreach}
    

    【讨论】:

      【解决方案2】:

      首先你应该修复你的 PHP 代码:

      $reports = array();
      while($x = mysql_fetch_assoc($result1))
      {
          $reports[] = array($x['ReportId'], $x['MailingFrequency'], 'Stop Subscription');
      }
      
      $this->smarty->assign('reports', $reports);
      

      你应该在 Smarty 中使用 {foreach}: http://www.smarty.net/docsv2/en/language.function.foreach.tpl

      {foreach from=$reports item=report}
          <tr> 
            <td width="25%">{$report[0]}</td>
            <td width="35%">{$report[1]}</td>
            <td width="40%"><a href="../webadmin/stopsubscription" id="StopSubscription" style="color: #CC0000">{$report[2]}</a></td>
          </tr>
      {/foreach}
      

      希望我能帮上忙。 :)

      【讨论】:

      • 哦,你的速度更快 :D
      • 哈哈,对不起。 :D
      • @walkingRed 感谢您的回复。
      • @RuchirSharma 检查 phpdocs 是否紧凑。它可能会帮助您将变量发送到模板。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2015-11-24
      • 1970-01-01
      • 2016-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多