I have a foreach loop returning bookings. The loop is returning the following object (not all the fields but easier to read):
object(stdClass)#28 (127) {
["trainee_total"]=> string(1) "1"
["trainee_id"]=> string(4) "1625"
["trainee_voyageID"]=> string(3) "151"
["trainee_berthID"]=> string(2) "25"
["trainee_sessionID"]=> string(3) "313"
["trainee_bookingReference"]=> string(13) "STV220910-001"
["trainee_firstName"]=> string(7) "Matthew"
["booking_id"]=> string(4) "1243"
["booking_status"]=> string(1) "0"
["booking_reference"]=> string(13) "STV220910-001"
["contact_id"]=> string(2) "52"
["contact_title"]=> string(2) "Mr"
["contact_firstName"]=> string(7) "Matthew"
}
I'm displaying this is in a table.
I'm trying to merge with rowspan's the "Booked by", "Invoices" and "Balance to pay" columns so that the table is clearer to the user. As per the example shown above the first three rows are related by one booking, and the final row is another booking.
As you can see from the data returned by my database call, the booking_* columns are returned alongside with the trainee_* data in each row.
I have a column "trainee_total" which returns a count of the rows per booking, this is returning 3, 3, 3, 1 in the case above.
I've tried to use the total to set a rowspan on the aforementioned columns but this obviously messes with the formatting of the table as it is rowspanning 3, 3, 3, 1.
I've then tried to add some logic behind it with the following code:
<table class="table table-bordered mt-4">
<thead>
<tr>
<th>
</th>
<th>
Participant
</th>
<th>
Age / DOB
</th>
<th>
Status
</th>
<th>
Booked by
</th>
<th>
Invoices
</th>
<th>
Balance to pay
</th>
<th>
Booking Form
</th>
</tr>
</thead>
<tbody>
<?php foreach ($trainees as $trainee) : ?>
<?php
$total = $trainees->total; $x = $total;
?>
<tr>
<?php if ($total == 0): ?>
<td>
the cell detail
</td>
<?php else: ?>
<?php if ($x == $total) : ?>
<td rowspan="<?php echo $total; ?>">
the cell detail
</td>
<?php endif; ?>
<?php $x++; endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This obviously wrecks the table as on each row iteration is resets the counters.
Is there a way to have some logic within the foreach that allows me to do what I want? For example look at the previous iteration of the loop and if the booking_reference matches then skip putting in that td?
I've kinda painted myself into a corner as I need to display each trainee in the table as separate entities.
