Connecting Tech Pros Worldwide Forums | Help | Site Map

Grouping results with PHP

Newbie
 
Join Date: Apr 2008
Posts: 3
#1: Apr 8 '08
Hello,

I'm a bit of a newcomer to PHP and am having an issue I can't overcome.

I've read some great posts on the issue of grouping results on this forum 1 and can happily display results under a common variable. The problem occurs as I want to display the common variable after the grouped results.

I'm attempting to write out the results of a query for journal articles in a citation format that lists authors before the journal title (common variable).

I have 3 tables.

Authors

aID | LastName
1 | Smith
2 | Jones
3 | Jacks

Pubs

pID | pTitle
1 | Article 1
2 | Article 2

AuthorsJoin
pID | aID
1 | 1
1 | 2
1 | 3

I'd like to output my results in the format

Jacks, Jones, Smith Article 1

I can currently output as the following using the code below.

Article 1 Jacks, Jones, Smith
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   LastName,
  3.   AuthorsJoin.aID,
  4.   Authors.aID,
  5.   AuthorsJoin.pID,
  6.   Pubs.pID,
  7.   PubTitle 
  8. FROM Authors, AuthorsJoin, Pubs
  9. WHERE Authors.aID=AuthorsJoin.aID
  10. AND AuthorsJoin.pID=Pubs.pID
  11. ORDER BY Pubs.PubTitle
  12.  
Expand|Select|Wrap|Line Numbers
  1. <?PHP
  2. $ptitle = "";
  3. while ($row = mysql_fetch_array($result))
  4. {
  5.   if (strcmp( $ptitle, $row['PubTitle']))
  6.   {
  7.     echo "<br /><strong>$ptitle</strong> ";
  8.     $ptitle = $row['PubTitle'];
  9.   }
  10.   echo $row['LastName'].', ';
  11. }
  12. ?>
  13.  
So the above will let me output the grouped results under a common variable, but I need something that will let me output the common variable after the grouped results... is this even possible?

Any help would be most appreciated!

Keven

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#2: Apr 8 '08

re: Grouping results with PHP


Hi. Welcome!

As I understand the code you posted, you would probably not get the output you gave us. It would probably look a bit more like:
"Article1 Jones, Article1 Smith, Article1 Otherguy"

In any case...
The easiest way to get the output you want would probably be to use two simple queries instead of one complex query.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. // Query all article names and ID's
  2. $sql = "SELECT articleID, articleName FROM articleTable";
  3. $articleResult = mysql_query($sql);
  4.  
  5. // Loop through all articles
  6. while($articleRow = mysql_fetch_assoc($articleResult))
  7. {
  8.   // Get all authors that match the article
  9.   $sql = "
  10.     SELECT at.authorName 
  11.     FROM authorTable AS at
  12.     INNER JOIN authorArticleList AS aal
  13.       ON aal.articleID = {$articleRow['articleID']}";
  14.   $authorResult = mysql_query($sql);
  15.  
  16.   // Print the names of all authors
  17.   while($authorRow = mysql_fetch_assoc($authorResult))
  18.   {
  19.     echo $authorRow['authorName'], " ,";
  20.   }
  21.  
  22.   // Print the name of the article
  23.   echo "<b>", articleRow['articleName'], "</b><br />";
  24. }
  25.  
This would give you a list of all authors for all articles, arranged as you suggested.
You would of course have to make this fit your code and databases.

P.S.
I've added [code] tags and re-formatted you query and code a bit to make it a little easier to read. Was careful not to change anything tho :P
Newbie
 
Join Date: Apr 2008
Posts: 3
#3: Apr 8 '08

re: Grouping results with PHP


Thanks Atli, This will definitely give me something to play with tomorrow. I appreciate your help!
Newbie
 
Join Date: Apr 2008
Posts: 3
#4: Apr 8 '08

re: Grouping results with PHP


I ended up adjusting the SQL a bit and it worked like a charm! Thanks again for the solution.
Expand|Select|Wrap|Line Numbers
  1. SELECT at.authorName
  2. FROM authorTable AS at
  3. INNER JOIN authorArticleList AS aal
  4. ON at.aID=aal.aID
  5. WHERE aal.articleID = {$articleRow['articleID']}
  6.  
Reply