I'm just wondering if you can help me organize the query result from many to many relationship, below are other details.
SELECT rt.room_title as rm_name, amn.amnty_title FROM amenities_tbl AS amn LEFT JOIN room_am_link AS ral ON amn.amnty_id = ral.am_id LEFT JOIN rooms_tbl AS rt ON ral.rm_id = rt.room_id;
and here is my foreach loop on the query result (codeigniter):
<?php foreach($result as $row){?>
<tr>
<td><?php echo $row['rm_name']; ?></td>
<td><?php echo $row['am_title']; ?></td>
</tr>
<?php } ?>
the display result is show below,
but what i want the display to be is like this image below, the room title is not repeated:
i'm sorry if my questions is very long, i just want it to be clear as much as possible.
thank you and hope you can help me guys.
simple way of doing this is to have a variable, that will keep value of the item from the first column, then in loop, you are displaying it only if value will change, eg.:
<?php
$first_col = NULL;
foreach($result as $row){ ?>
<tr>
<td><?php
if ($first_col != $row['rm_name']) {
echo $row['rm_name'];
$first_col = $row['rm_name'];
} else {
echo " ";
}; ?>
</td>
<td><?php echo $row['am_title']; ?></td>
</tr>
<?php } ?>
of course, your data should be sorted by this first column - you need to add ORDER BY
to your SQL.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments