473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Grouping results with PHP

3
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
Apr 7 '08 #1
3 1479
Atli
5,058 Expert 4TB
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
Apr 8 '08 #2
kevenj
3
Thanks Atli, This will definitely give me something to play with tomorrow. I appreciate your help!
Apr 8 '08 #3
kevenj
3
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.  
Apr 8 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Debbie Davis | last post by:
Hi there, SQL 2000 I have the following query: SELECT sponsor, COUNT(sponsor) * 2 AS total FROM Referrals GROUP BY sponsor Works great, returns the sponsor and the total * 2 of their...
3
by: Graham | last post by:
Hi, I am having trouble getting XSL to count the members of a group. What I am trying to do is group by <objectid.Contactid> and count the number of <activityid>'s for each <objectid.contactid>....
2
by: Tristan Miller | last post by:
Greetings. I have a question involving sorting and grouping output using XSLT. I am trying to produce a publication list from a BibTeX-like XML file: <bibxml:file...
4
by: kristofera | last post by:
I am trying to do a distinct grouping of some nodes sorted by a numeric value but for some reason the distinct (preceding-sibling filter) is applied to the result as if not sorted. If I don't use...
3
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
4
by: VSS | last post by:
Hello, Under the 'Current Status' footer i have the following control source: =Count(*) This tells me the sub total of rows under each 'current status' grouping. Under the Assigned team...
9
by: JakeTheSnake | last post by:
Hello, I'm new here, but am really impressed with the positive attitude! I was wondering if someone could give me some help or point me in the right direction. I have a query that returns the...
4
by: Chris | last post by:
I tried to retrieve the digit grouping symbol in MSAccess but unfortunately 3;0 is retrieved instead of comma which is my symbol. Function retrieves decimal symbol and list separator without any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.