ferdinand
ferdinand

Reputation: 409

how to set rowspan table use foreach schema in PHP code

now im doing PHP code for display report using table with foreach. but i still confuse how to set rowspan using foreach. i want the display like this image

enter image description here

if i do it manualy, the HTML code should be like this.

<table> 
    <thead>
      <tr>
        <th >IMAGE</th>
        <th >Name</th>
        <th >Accessories</th>
        <th >Qty</th>
        <th >Action</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td  rowspan="2"><img src=""></td>
        <td  rowspan="2">John</td>
        <td >Cable</td>
        <td >1</td>
        <td  rowspan="2">Delete</td>
      </tr>
      <tr>
        <td >standart</td>
        <td >3</td>
      </tr>
      <tr>
        <td  rowspan="3"><img src=""></td>
        <td  rowspan="3">james</td>
        <td >screen</td>
        <td >1</td>
        <td  rowspan="3">Delete</td>
      </tr>
      <tr>
        <td >CPU</td>
        <td >1</td>
      </tr>
      <tr>
        <td >memory</td>
        <td >2</td>
      </tr>
    </tbody>
    </table>

but how do i implement that using foreach. here is what i implement it.

<table width="100%" >
    <thead>
        <tr>
          <th >Image</th>
          <th >Name</th>
          <th >Accessories</th>
          <th >Qty</th>
          <th >Action</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($reports as $report)
        <tr>
            <td class="text-center"><img src="{{ $report->image }}"></td>
            <td class="text-center">{{ $report->name }}</td>
            <td class="text-center">{{ $report->accessories }}</td>
            <td class="text-center">{{ $report->qty }}</td>
            <td class="text-center"><button>delete</button></td>
        </tr>
        @endforeach
    </tbody>
</table>

i want rowspan number on "image" and row "name" Adjust the number of "accessories" data. please help.

Upvotes: 1

Views: 2726

Answers (1)

Barmar
Barmar

Reputation: 781974

Create a new array where you group all the rows with the same name. Then you can get the length of the nested accessories array so you'll know the correct value of rowspan

$grouped_reports = [];
$last_name = null;
$group = null;
foreach ($reports as $report) {
    if ($report->name != $last_name) {
        if ($group) {
            $grouped_reports[] = $group;
        }
        $group = ['name' => $report->name, 'image' => $report->image, 'accessories' = []];
    }
    $group['accessories'][] = ['name' => $report->accessories, 'qty' => $report->qty];
}
// Add last group to $grouped_reports
if ($group) {
    $grouped_reports[] = $group;
}

...
<tbody>
@foreach ($grouped_reports as $report) {
    @$rowspan = count($report['accessories']);
    <tr>
        <td class="text-center" rowspan="{{ $rowspan }}"><img src="{{ $report['image'] }}"></td>
        <td class="text-center" rowspan="{{ $rowspan }}">{{ $report['name'] }}</td>
        <td class="text-center">{{ $report['accessories'][0]['name'] }}</td>
        <td class="text-center">{{ $report['accessories'][0]['qty'] }}</td>
        <td class="text-center" rowspan="{{ $rowspan }}"><button>delete</button></td>
    </tr>
    @for ($i = 1; $i < $rowspan; $i++) {
    <tr>
        <td class="text-center">{{ $report['accessories'][$i]['name'] }}</td>
        <td class="text-center">{{ $report['accessories'][$i]['qty'] }}</td>
    </tr>
    @endfor
    @endforeach
</tbody>

Upvotes: 1

Related Questions