Jump to content

Archived

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

MissAli

Help displaying results from joined tables

Recommended Posts

Hi,

I am cuurently being a site that lists publications and their authors. My problem is displaying the results of my databases.

I have 3 tables publications, people and peoplexpublications

Publications - contains all the info about the publication, title year etc and publication_code

People - contains a list of people and people_code

peoplexpublications - contains the publication_code and people_code when a person is associated with a publication, this table can contain one publication_code to many people_codes as more than one person can be a part of a publication.

My problem is when i try to display all this information from my 3 tables.
here is the code
[code]

$sql = "
SELECT *
FROM publications
WHERE publications.publication_status = 'live'


ORDER BY publications.publication_year DESC
";
$result = mysql_query($sql, $conn)or die(mysql_error());

// Set variables from the query for use in the html
while ($row =mysql_fetch_array($result,MYSQL_ASSOC)){
    $publication_code = $row['publication_code'];
    $publication_type=$row['publication_type'];
    $publication_summary=$row['publication_summary'];
    $publication_title=$row['publication_title'];
    $publication_year=$row['publication_year'];
    $pkey = $row['pkey'];
    $publication_status=$row['publication_status'];

$sql = "
SELECT *
FROM peoplexpublications as pxp, people
WHERE pxp.publication_code = $publication_code
AND pxp.people_code = people.people_code


";
$result2 = mysql_query($sql, $conn)or die(mysql_error());


while ($row2 =mysql_fetch_array($result2,MYSQL_ASSOC)){
$peoplexpublication_id=$row2['peoplexpublication_id'];
    $people_code=$row2['people_code'];
    $last_name =$row2['last_name'];
    
    
    
    
$display_block .= "<p>$last_name, $publication_title,$publication_type,(<i>$publication_year</i>)</p>";
}

}    
[/code]


Nowthis is producing a result that looks like this:

De Souza, a publications teste,Peer Review,(2006)

Craig, a publications teste,Peer Review,(2006)

Murnaghan, a publications teste,Peer Review,(2006)

De Souza, zebra zebra,zebra,(2005)

Craig, zebra zebra,zebra,(2005)

De Souza, PostGrad Couses 2006,Book Chapters,(2005)

Craig, PostGrad Couses 2006,Book Chapters,(2005)

Gallagher, PostGrad Couses 2006,Book Chapters,(2005)

Murnaghan, PostGrad Couses 2006,Book Chapters,(2005)

Craig, Study of a woman on mars,Paper,(2005)

Regan, Study of a woman on mars,Paper,(2005)

Prenter, Study of a woman on mars,Paper,(2005)

Cassidy, Study of a woman on mars,Paper,(2005)

Murphy, Study of a woman on mars,Paper,(2005)

However i would like to group all the names that are associated with the same publication together on one line and display that publication once only. So in this example i would see four titles only.

Anyone , any suggestions

Thnaks in advance

Share this post


Link to post
Share on other sites
Try something like this.
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] *, GROUP_CONCAT(last_name [color=green]ORDER BY[/color] last_name) [color=green]AS[/color] names
[color=green]FROM[/color] [color=orange]peoplexpublications[/color] [color=green]as[/color] pxp, people
[color=green]WHERE[/color] pxp.publication_code [color=orange]=[/color] $publication_code [color=blue]AND[/color] pxp.people_code [color=orange]=[/color] people.people_code GROUP BY pxp.publication_code [!--sql2--][/div][!--sql3--]

Then you'll have the last names grouped together as a comma separated string in $row['names']

Share this post


Link to post
Share on other sites

×

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.