ManiMuthuPandi
ManiMuthuPandi

Reputation: 1604

two dimentional array in smarty

I am trying to display a two dimensional array in smarty like this

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); 

Each row should display 5 columns(td). But this code displays one td per row

Upvotes: 0

Views: 37

Answers (1)

Jirka Hrazdil
Jirka Hrazdil

Reputation: 4021

Change your while loop to:

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;
}

In your version of the loop, you were creating five "rows" in the membership array per iteration, instead of one "row" with five "columns".

Upvotes: 2

Related Questions