473,804 Members | 3,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grouping results with PHP

3 New Member
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 1503
Atli
5,058 Recognized Expert Expert
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 New Member
Thanks Atli, This will definitely give me something to play with tomorrow. I appreciate your help!
Apr 8 '08 #3
kevenj
3 New Member
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
1878
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 referrals
3
1815
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>. My XSL keeps returning zero for the count. The results for the XML/XSLT files below should look like:- Contact # of Visits Account ----------------------------------------------------------
2
2736
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 xmlns:bibxml="http://bibtexml.sf.net/"> <bibxml:entry id="foo01"> <bibxml:article> <bibxml:title>Foo</bibxml:title>
4
3571
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 the preceding-sibling filter, the nodes are properly sorted. Is this a bug in the xslt processor I'm using (.net framework 1.1) or is this correct behaviour? Any alternative solutions that will show me the lowest priced item in each category?...
3
2745
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 reports at the plan (overall totals), department and division levels which have sorting and grouping implemented with this new
8
3539
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 the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word templates, and the queries that drive them, depending on what events a course has.
4
1327
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 member footer, I have the same as the above control source. This tells me how many rows in total each assigned team member has.
9
1840
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 following fields: city state name serial category using this code
4
3859
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 problem. I used the code below: var_char_copied = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SGROUPING, var_SYSTEM_DTG, Len(var_SYSTEM_DTG))
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7615
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.