smarty中的二维数组

two dimentional array in smarty

我正在尝试像这样在 smarty 中显示二维数组

聪明HTML

<table>
{foreach from=$periods item=table}
  <tr>
  {foreach from=$table item=val}
    <td>{$val}</td>
  {/foreach}
  </tr>
{/foreach}
</table> 

PHP

$membership=array();
  $i=0;
  while ($dao->fetch()) {
    $membership[]['sno']=++$i;
    $membership[]['start_date']=$dao->start_date;
    $membership[]['end_date']=$dao->end_date;
    $membership[]['total_amount']=$dao->total_amount;
    $membership[]['membership_id']=$dao->membership_id;
    }
    $this->assign('periods',$membership); 

每行应显示 5 列 (td)。但是这段代码每行显示一个 td

将您的 while 循环更改为:

while ($dao->fetch()) {
    $m = [];
    $m['sno']=++$i;
    $m['start_date']=$dao->start_date;
    $m['end_date']=$dao->end_date;
    $m['total_amount']=$dao->total_amount;
    $m['membership_id']=$dao->membership_id;
    $membership[] = $m;
}

在您的循环版本中,您每次迭代在 membership 数组中创建五个 "rows",而不是一个 "row" 和五个 "columns"。