Smarty:同一行显示不止一次

Smarty: Same row getting displayed more than once

这是我的 PHP 代码(returns 行有 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'),它会正确显示。但是,在多行的情况下,只显示最后一行。 如何显示htmltable?

中的所有行

首先你应该修复你的 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}

希望我有所帮助。 :)

在您的代码中,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}