Jump to content

Help with query/output for one to many.


sledge4

Recommended Posts

I have two tables, for which I want to output data from.  Here's an example of what I am trying to accomplish.  The ID's link/join each table.

 

Table 1 (ID | Name)

1 | John

2 | Bob

3 | Sara

 

Table 2 (ID | Type)

1 | Soda

1 | Candy

2 | Beer

2 | Ice Cream

3 | Popcorn

 

I'm trying to output the data like this:

 

John - Soda, Candy

Bob - Beer, Ice Cream

Sara - Popcorn

 

Instead I get this

 

John - Soda

John - Soda

Bob - Beer

Bob - Ice Cream

Sara - Popcorn

 

Any point in the right direction would be GREATLY appreciated.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/137070-help-with-queryoutput-for-one-to-many/
Share on other sites

Ok, group_concat worked...thanks!  But I have another question.  Is it possible to link each result?

 

Here's my code:

 

<?
$query = mysql_query("SELECT a_site_residents.name AS Resident, GROUP_CONCAT(a_site_stables.stable ORDER BY a_site_stables.stable ASC SEPARATOR ', ') AS Stable FROM a_site_residents, a_site_stables WHERE a_site_residents.id=a_site_stables.id GROUP BY a_site_residents.name");
$numrows=@mysql_num_rows($query);
if($numrows != 0) {
while ($result = mysql_fetch_array($query)) {
?>

<table border="1" width="100%">
<tr>
	<td width="100" valign="top"><? echo $result['Resident']; ?></td>
	<td><? echo $result['Stable']; ?></td>
</tr>
</table>

<? } } ?>

 

 

What I'm wondering if I can do is link each "Stable" result that is being output.

 

Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.