PHP generate rowspan dynamically

816 views Asked by At

I have following item list fetch from table, I need to add dynamic rowspan at the end of the row if item is from same supplier, but I have no idea how to work with this.

I tried:

foreach($items as $item){

    /*get total num for rowspan*/
    $group = $buyer->get_total_rowspan($obj->id, $obj->supplier);

    echo '<tr>
             <td>$item->id</td>
             <td>$item->name</td>
             <td>$item->supplier</td>';

          if($group->countRow > 1){
             <td rowspan="$group->countRow"><a>Manage</a></td>
          }

      if($group->countRow > 1){
          echo '<td rowspan="'.$group->countRow.'"><a>manage</a></td>';
      }else{
          echo '<td><a>test</a></td>';
      }

    echo '</tr>';
}

but cell Manage will always appear at every row with mess format.

the idea results that I want:

enter image description here

1

There are 1 answers

3
Flo On

You can try something like that:

$lastId = null;
foreach($items as $item){

    /*get total num for rowspan*/
    $group = $buyer->get_total_rowspan($obj->id, $obj->supplier);

    echo '<tr>
             <td>$item->id</td>
             <td>$item->name</td>
             <td>$item->supplier</td>';

          if($lastId != $group->Id){
             <td rowspan="$group->countRow"><a>Manage</a></td>
          }

    echo '</tr>'

    $lastId = $group->Id;
}

Everytime there is a new group you can set the $i back to 0.