Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:08:30+00:00 2026-06-14T11:08:30+00:00

i have a problem with an association between 2 tables: i have table 1

  • 0

i have a problem with an association between 2 tables:
i have

table 1 "flowers": "id", "flower";
table 2 "colors": "id", "color";
table 3 "association_flowers_colors": "id", "id_flower", "id_color";

now, a single flower can have many colors, now the problem is the father all the informations inside a single array.
es: if i get all the informations inside the first table and fetch them i’m gonna have

array([0](array[id]=>0, [flower]=>rose), [1]array([id]=>1, [flower]=>tulip));

then if i gather all the informations from the second table i’m gonna have

array([0]array([id]=>0, [color]=>red)[1]), [1]array([id]=>1, [color]=>white));

Now to the problem: i’d need to get an array along the lines of

array([0]array([id]=>0, [flower]=>rose, [color]=>red, [color]=>white), [1]array([id]=>1, [flower]=>tulip, [color]=>red, [color]=>white));

is there a simple way to do it with a single elaborated query or should i go with while loop or something like that and then merge the informations i get?

Thanks

edit:

function get_flowers(){
    include('../actions/db_connection.php');
        $result = mysqli_query($db_connection, "select * 
            from flowers f
            left outer join association_flowers_colors a on f.id = a.id_flower
            left outer join colors c on c.id = a.id_color");
        mysqli_close($db_connection);
         $flowers = array();
          while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
         $flowers[] =  $row;
        };

    return $flowers;
}

I ended up with this after reading juergen’s reply, it looks like it works the problem is that it gives me an array for each color(for a total of 4 arrays) instead of giving me only 2 arrays with both colors inside(for a total of 2 arrays).

edit2:
Ok, i didnt find a way to retrieve all the informations in 2 arrays(instead of 4) but i managed to print them in a readable way anyway:

while($row = mysqli_fetch_array($flowers)){


    if ($flowerID != $row['id_flower']) {
        echo "<div class='flowerContainer'>";
        echo "<input type='hidden' name='flowerID' value='".$row['id_flower']."'>";
        echo "Name: ".$row['flower']."<br />";
        }

    echo $row['color']."<br />";
    if ($flowerID == $row['id_flower']) {
        echo "</div>";
        echo "<hr>";
    }

    $flowerID = $row['id_flower'];
    }

in this way even if the loop repeats all the information for every array, it will only output the information if there’s a new entry and it won’t repeat anything.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:08:32+00:00Added an answer on June 14, 2026 at 11:08 am
    select * 
    from flowers f
    left outer join association_flowers_colors a on f.id = a.id_flower
    left outer join colors c on c.id = a.id_color
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem :( I have a many-many table between two tables(1&2), via
I have a problem in association between two classes, so i have a class
I have a table called SecurityContacts and another table called Contacts. The association between
I have a problem with fetching data from DB, where is between models association
I am trying to create an association between two tables. A student table and
I have two MySQL tables A and B . Table A has a member
I am having problems setting up an association in entity framework between two tables
I have two objects. Arrangement and InvolvedParty. There is an association between Arrangement and
We have 2 tables: Table Authority: public class Authority { public int ID {get;set;}
I am using NHibernate and have a many-to-many association between an Employee and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.