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 -
SELECT
-
LastName,
-
AuthorsJoin.aID,
-
Authors.aID,
-
AuthorsJoin.pID,
-
Pubs.pID,
-
PubTitle
-
FROM Authors, AuthorsJoin, Pubs
-
WHERE Authors.aID=AuthorsJoin.aID
-
AND AuthorsJoin.pID=Pubs.pID
-
ORDER BY Pubs.PubTitle
-
-
<?PHP
-
$ptitle = "";
-
while ($row = mysql_fetch_array($result))
-
{
-
if (strcmp( $ptitle, $row['PubTitle']))
-
{
-
echo "<br /><strong>$ptitle</strong> ";
-
$ptitle = $row['PubTitle'];
-
}
-
echo $row['LastName'].', ';
-
}
-
?>
-
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
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: -
// Query all article names and ID's
-
$sql = "SELECT articleID, articleName FROM articleTable";
-
$articleResult = mysql_query($sql);
-
-
// Loop through all articles
-
while($articleRow = mysql_fetch_assoc($articleResult))
-
{
-
// Get all authors that match the article
-
$sql = "
-
SELECT at.authorName
-
FROM authorTable AS at
-
INNER JOIN authorArticleList AS aal
-
ON aal.articleID = {$articleRow['articleID']}";
-
$authorResult = mysql_query($sql);
-
-
// Print the names of all authors
-
while($authorRow = mysql_fetch_assoc($authorResult))
-
{
-
echo $authorRow['authorName'], " ,";
-
}
-
-
// Print the name of the article
-
echo "<b>", articleRow['articleName'], "</b><br />";
-
}
-
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
Thanks Atli, This will definitely give me something to play with tomorrow. I appreciate your help!
I ended up adjusting the SQL a bit and it worked like a charm! Thanks again for the solution. -
SELECT at.authorName
-
FROM authorTable AS at
-
INNER JOIN authorArticleList AS aal
-
ON at.aID=aal.aID
-
WHERE aal.articleID = {$articleRow['articleID']}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
----------------------------------------------------------
|
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>
|
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?...
|
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
| |
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.
|
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.
|
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
|
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))
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
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
| |
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...
| |