Jump to content


Photo

Help displaying results from joined tables


  • Please log in to reply
1 reply to this topic

#1 MissAli

MissAli
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 23 May 2006 - 04:28 PM

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

$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>";
}

}    


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

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 23 May 2006 - 05:43 PM

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 ORDER BY last_name) AS names
FROM peoplexpublications as pxp, people
WHERE pxp.publication_code = $publication_code AND pxp.people_code = 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']
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users