聪明的二维数组

时间:2017-07-08 19:22:52

标签: php 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

1 个答案:

答案 0 :(得分:2)

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数组中创建了五个“行”,而不是一个带有五个“列”的“行”。