php mysql many to many relationship query result display

ytse_jam

I'm just wondering if you can help me organize the query result from many to many relationship, below are other details.

enter image description here
here is my query:

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,
enter image description here

but what i want the display to be is like this image below, the room title is not repeated:
enter image description here

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.

Jerzyk

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 "&nbsp;";
             }; ?>
      </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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL, join many to many relationship query

From Dev

Trying to display in a simple php system mysql data with One to many relationship?

From Dev

Many to many relationship and MySQL

From Dev

Mysql Many to Many relationship

From Dev

Specific result in many to many relationship

From Dev

PHP Query To Filter Many-To-Many MySQL

From Dev

Query many to many relationship with DetachedCriteria

From Dev

how to query a many to many relationship?

From Dev

eloquent: query many to many relationship

From Dev

How to query for many to many relationship between products and filters in MySQL?

From Dev

How to query a pivot table data in MySQL (many to many relationship)

From Dev

denormalize many to many relationship in MySQL

From Dev

JPA criteria query in a many-to-many relationship

From Dev

Parse - Array of pointers - Many to Many relationship query

From Dev

CoreData: Query to one-to-many-to-many relationship

From Dev

Many-to-many relationship query alright?

From Dev

Advanced SQLite query on many-to-many relationship

From Dev

Laravel Many to Many query relationship with where clause

From Dev

Hibernate query on many to many relationship with extra column

From Dev

JPQL query with many-to-many relationship

From Dev

ios parse one query many to many relationship

From Dev

Linq Query relating Many to Many relationship

From Dev

Many to Many relationship query. Laravel

From Dev

query over many to many relationship in redbeanphp

From Dev

Laravel Many to Many query relationship with where clause

From Dev

Unable to query over many-to-many relationship

From Dev

HQL query for Many to Many Explict relationship

From Dev

Single SQL query on many to many relationship

From Dev

Laravel many-to-many relationship query

Related Related

  1. 1

    MySQL, join many to many relationship query

  2. 2

    Trying to display in a simple php system mysql data with One to many relationship?

  3. 3

    Many to many relationship and MySQL

  4. 4

    Mysql Many to Many relationship

  5. 5

    Specific result in many to many relationship

  6. 6

    PHP Query To Filter Many-To-Many MySQL

  7. 7

    Query many to many relationship with DetachedCriteria

  8. 8

    how to query a many to many relationship?

  9. 9

    eloquent: query many to many relationship

  10. 10

    How to query for many to many relationship between products and filters in MySQL?

  11. 11

    How to query a pivot table data in MySQL (many to many relationship)

  12. 12

    denormalize many to many relationship in MySQL

  13. 13

    JPA criteria query in a many-to-many relationship

  14. 14

    Parse - Array of pointers - Many to Many relationship query

  15. 15

    CoreData: Query to one-to-many-to-many relationship

  16. 16

    Many-to-many relationship query alright?

  17. 17

    Advanced SQLite query on many-to-many relationship

  18. 18

    Laravel Many to Many query relationship with where clause

  19. 19

    Hibernate query on many to many relationship with extra column

  20. 20

    JPQL query with many-to-many relationship

  21. 21

    ios parse one query many to many relationship

  22. 22

    Linq Query relating Many to Many relationship

  23. 23

    Many to Many relationship query. Laravel

  24. 24

    query over many to many relationship in redbeanphp

  25. 25

    Laravel Many to Many query relationship with where clause

  26. 26

    Unable to query over many-to-many relationship

  27. 27

    HQL query for Many to Many Explict relationship

  28. 28

    Single SQL query on many to many relationship

  29. 29

    Laravel many-to-many relationship query

HotTag

Archive