How to display n -n relationship in php : Many to many relationship

If I have following tables

Many to many relationship

Category A
ID , CategoryName


ID_CategorB , FK_CategoryA, CategoryName

Third Table Relationship Table

ID ,ID_CategoryB

How can I show the values in "select "  box
Puneet AroraFounderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
ID_A     -- PK

ID_B     -- PK

Table_A_B      <-- this is known as bridge table
ID_A     -- FK
ID_B     -- FK

You can make write SQLs you need from these table like:

Select a.CategoryName as Cat_A, b.CategoryName As Cat_B
From Table_CategoryA a inner join Table_A_B ab
on a.ID_A = ab.ID_A inner join Table_CategoryB b
on ab.ID_B = b.ID_B

Open in new window

Puneet AroraFounderAuthor Commented:
Thanks for the reply :

I want   to show it in select box , then uses these multiple values
to select some more in the Category  C table , In php do I need to simply use for loop
to get all the values  and show in "select option box " and use java script to transfer
it to input box with multiple values ? Question is How to ?
In php you need to query the db and iterate over the records using a while loop. Since you seem to need just the <option> tags, echo them directly within the while -construct:

	$mysqli = new mysqli("$host", "$username", "$password", "$db_name");

	/* check connection */
	if (mysqli_connect_errno()) {
	    printf("Connect failed: %s\n", mysqli_connect_error());

	// The way this is setup, on the client-side you need to send two fields:
	// category: This should have the value that the user selected on the first list
	// ajax-request: you can set this to anything.  It is just meant to allow the
	// if clause below to execute and give you only the <option>s for the second dropdown
	if( array_key_exists('ajax-request',$_POST) )
		$catcode = $_POST['categoryA'];
		$sql ="SELECT B.ID_CategoryB as `B_ID`, B.CategoryName as `B_CategoryName`
				FROM `CategoryB` B INNER JOIN `ThirdTable` C ON B.ID_CategoryB=C.ID_CategoryB
				WHERE B.FK_CategoryA=? ORDER BY B.CategoryName ASC";
		$stmt = $mysqli->prepare($sql);
			// assuming that the value you are passing from the first select 
			// is an integer, then use "i" as the first argumen to bind_param()
			// If it is a string, use "s" instead
			$stmt->bind_param("i", $_POST['category']);
			$stmt->bind_result($B_ID, $B_CategoryName);
			while ( $stmt->fetch() ) {
				echo sprintf('<option value="%s">%s</option>', $B_ID, htmlentities($B_CategoryName, ENT_QUOTES, "UTF-8"));

Open in new window

The ajax callback function will "see" just the options and append them to the second drop down (like the example I referred you in the other question).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.