Hi,
Can anybody help me with a many-to-many join output problem? I need to grab info from a main table, and assocatiated authority list, then join it with an expanding list of topics. This is okay, but I want the output of the mulitple hits from the many to many topics table to output in an array, THEN go to the next result row. is such a thing possible?
a simplified mockup version of the tables:
table1
name (boston, chicago, new york)
stateitem (MA, IL, NY)
id (1, 2, 3)
table2
stateid (MA, IL, NY)
statename (Massachussets, Illinois, New York)
table3
t1id topic
2 windy
2 jazz
2 gangsters
3 broadway
3 chinatown
1 chinatown
2 chinatown
1 jazz
3 jazz
the idea is that I can just keep adding topics to table 3 whenever needed and be able to look up the associated info in the other tables. (note all of the tables have a separate auto incremented ID, and "table1.id" & "table3.t1id" here are both just kept static, even if records are deleted later.)
what I want to do is to send a text string query against some info in table1.name and have the multiple items that are related to each entry in table1 appear on a single line in the output, as in:
query: new york
desired output:
name: new york
statename: New York
topic: broadway, chinatown, jazz
I am using something simple like this:
begin PHP snippet>>>>>>>>>>>>>
$result = mysql_query ("SELECT * FROM table1, table2, table3
WHERE table1.stateitem = table2.stateid
AND table1.id = table3.t1id
AND table1.name LIKE '$name%'
AND table2.statename LIKE '$statename%'
AND table3.topic LIKE '$topic%'
");
if ($row = mysql_fetch_array($result)) {
do {
print ("Name:");
print $row["table1.name"];
print ("<br>");
print ("State:");
print $row["table2.statename"];
print ("<br>");
print ("Topic:");
print $row["table3.topic"];
print ("<p><p>");
} while($row = mysql_fetch_array($result));
} else {print "Sorry, no records were found!";}
end PHP snippet>>>>>>>>>>>>>>>>
using this code we get all of the possible output as separate listings as in:
query: new york
output:
name: new york
statename: New York
topic: broadway
name: new york
statename: New York
topic: chinatown
name: new york
statename: New York
topic: jazz
is there any way to write an "if / else", "if / while", or other loop in PHP to get the desired output of the extra matches on a single line?
thanks for any ideas!
alexei