Smarty:同一行不止一次显示

时间:2015-03-30 10:18:41

标签: php html mysql html-table smarty

这是我的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,'星期六'),它会正确显示。但是,如果有多行,则只显示最后一行。 如何显示html表中的所有行?

2 个答案:

答案 0 :(得分: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}
希望我能帮到你。 :)

答案 1 :(得分:2)

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