473,396 Members | 1,749 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 of query

Hi;

I'm brand new to PHP (just starting today to convert tons of
ColdFusion/Access code to PHP/MySQL). There is a function in
Coldfusion that I can't find an equivalent to in PHP. Here is
what I am trying to do:

If I have a query that results in this information:
State Firstname Lastname
Idaho Terry Pratchet
Vermont Dan Brown
Vermont Bob Smith
Virginia Alice Johnson
Virginia David Jones

I would like to output the info like this:

Idaho
Terry Pratchet
Vermont
Dan Brown
Bob Smith
Virginia
Alice Johnson
David Jones

But all I can find in PHP is a way to loop through the query
and show every row... if you are familiar with Coldfusion,
what I am looking for is something similar to this:

<CFOUTPUT GROUP='State'>
#State#<BR>
<CFOUTPUT>
#Firstname# #Lastname#
</CFOUTPUT>
<BR>
</CFOUTPUT>

Is there a function that does this in PHP, or do I need to
write code that watches the value of 'State' and manually
takes care of things whenever it changes?

Thanks for the help.

Jul 17 '05 #1
6 3606

Curt Bousquet wrote:
Hi;

I'm brand new to PHP (just starting today to convert tons of
ColdFusion/Access code to PHP/MySQL). There is a function in
Coldfusion that I can't find an equivalent to in PHP. Here is
what I am trying to do:

If I have a query that results in this information:
State Firstname Lastname
Idaho Terry Pratchet
Vermont Dan Brown
Vermont Bob Smith
Virginia Alice Johnson
Virginia David Jones

I would like to output the info like this:

Idaho
Terry Pratchet
Vermont
Dan Brown
Bob Smith
Virginia
Alice Johnson
David Jones

But all I can find in PHP is a way to loop through the query
and show every row... if you are familiar with Coldfusion,
what I am looking for is something similar to this:

<CFOUTPUT GROUP='State'>
#State#<BR>
<CFOUTPUT>
#Firstname# #Lastname#
</CFOUTPUT>
<BR>
</CFOUTPUT>

Is there a function that does this in PHP, or do I need to
write code that watches the value of 'State' and manually
takes care of things whenever it changes?

Thanks for the help.


your table looks strange, but i suppose it's just the tabs.

mysql takes care of your problem, have a look at SELECT and GROUP BY

micha

Jul 17 '05 #2
On 28 Apr 2005 01:13:08 -0700, micha wrote:
Curt Bousquet wrote:
Idaho Terry Pratchet
Vermont Dan Brown
Vermont Bob Smith
Virginia Alice Johnson
Virginia David Jones

I would like to output the info like this:

Idaho
Terry Pratchet
Vermont
Dan Brown
Bob Smith
Virginia
Alice Johnson
David Jones


mysql takes care of your problem, have a look at SELECT and GROUP BY


No it doesn't, unless you propose to have separate queries for each
state. So Curt will have to do something like:

$state = '';
while ( $row = mysql_fetch_array( $res ) )
{
if ( strcmp( $state, $row['state'] ) )
{
$state = $row['state'];
echo $state."\n";
}
echo "\t{$row['firstname']} {$row['lastname']}\n";
}
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #3
Curt Bousquet wrote:
Hi;

I'm brand new to PHP (just starting today to convert tons of
ColdFusion/Access code to PHP/MySQL). There is a function in
Coldfusion that I can't find an equivalent to in PHP. Here is
what I am trying to do:

If I have a query that results in this information:
State Firstname Lastname
Idaho Terry Pratchet
Vermont Dan Brown
Vermont Bob Smith
Virginia Alice Johnson
Virginia David Jones

I would like to output the info like this:

Idaho
Terry Pratchet
Vermont
Dan Brown
Bob Smith
Virginia
Alice Johnson
David Jones


I've never seen one discussed, so assuming it does not exist, you would have
to make one. There are two ways. The first is to make many trips to the
server as in (With postgress examples):

$results = pg_query("SELECT state from table group by state");
$rows = pg_fetch_all($results);
$answer = array();
foreach ($rows as $row) {
$results = pg_query(
"SELECT firstname,lastname
FROM states where state=".$row["state"]);
$answer[$row["state"]] = pg_fetch_all($results);
}

Second possibility would be to pull the whole thing down, and then walk
through it like so:

$results = pg_query("SELECT * from table group by state");
$answer = array();
while ($row = pg_fetch_array($results)) {
$state = $row["state"];
if (!isset($answer[$state])) {
$answer[$state"] = array();
}

$answer[$state][] = $row;
}
Either one of these could be made into a generalized routine. Send us your
result by clacks.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #4
On 28 Apr 2005 01:13:08 -0700, "micha" <ch*********@web.de> wrote:

Curt Bousquet wrote:
Hi;

I'm brand new to PHP (just starting today to convert tons of
ColdFusion/Access code to PHP/MySQL). There is a function in
Coldfusion that I can't find an equivalent to in PHP. Here is
what I am trying to do:

If I have a query that results in this information:
State Firstname Lastname
Idaho Terry Pratchet
Vermont Dan Brown
Vermont Bob Smith
Virginia Alice Johnson
Virginia David Jones

I would like to output the info like this:

Idaho
Terry Pratchet
Vermont
Dan Brown
Bob Smith
Virginia
Alice Johnson
David Jones

But all I can find in PHP is a way to loop through the query
and show every row... if you are familiar with Coldfusion,
what I am looking for is something similar to this:

<CFOUTPUT GROUP='State'>
#State#<BR>
<CFOUTPUT>
#Firstname# #Lastname#
</CFOUTPUT>
<BR>
</CFOUTPUT>

Is there a function that does this in PHP, or do I need to
write code that watches the value of 'State' and manually
takes care of things whenever it changes?

Thanks for the help.


your table looks strange, but i suppose it's just the tabs.

mysql takes care of your problem, have a look at SELECT and GROUP BY


GROUP BY is not required unless you're performing aggregate functions
such as min(), max(), count(), etc.

An ORDER BY in the query and, as the OP suggests, code that outputs a
new State whenever it changes is the solution.

--
David ( @priz.co.uk )
Jul 17 '05 #5
Kenneth Downs <kn**************@see.sigblock> wrote let it be
known in news:l1************@pluto.downsfam.net:
Curt Bousquet wrote:

I would like to output the info like this:

Idaho
Terry Pratchet
Vermont
Dan Brown
Bob Smith
Virginia
Alice Johnson
David Jones


Second possibility would be to pull the whole thing down,
and then walk through it like so:

$results = pg_query("SELECT * from table group by state");
$answer = array();
while ($row = pg_fetch_array($results)) {
$state = $row["state"];
if (!isset($answer[$state])) {
$answer[$state"] = array();
}

$answer[$state][] = $row;
}
Either one of these could be made into a generalized
routine. Send us your result by clacks.


Everybody knows the post office is faster than clacks... but
thanks. I got it working with code that is a combination of
what you posted and the idea from Ewoud. I was hoping that
there would be an 'elegant' solution built into PHP (which is
why I wasted hours Googling for such a solution), but I guess
this brute force method does the trick :)

I need to use this code a LOT, so am going to work on fitting
it into a function that I can call by passing it the name of
the field to watch and the HTML to output when that field
changes. If I get it all worked out, I'll post the results
here.
Jul 17 '05 #6
Curt Bousquet wrote:
Kenneth Downs <kn**************@see.sigblock> wrote let it be
known in news:l1************@pluto.downsfam.net:
Curt Bousquet wrote:

I would like to output the info like this:

Idaho
Terry Pratchet
Vermont
Dan Brown
Bob Smith
Virginia
Alice Johnson
David Jones


Second possibility would be to pull the whole thing down,
and then walk through it like so:

$results = pg_query("SELECT * from table group by state");
$answer = array();
while ($row = pg_fetch_array($results)) {
$state = $row["state"];
if (!isset($answer[$state])) {
$answer[$state"] = array();
}

$answer[$state][] = $row;
}
Either one of these could be made into a generalized
routine. Send us your result by clacks.


Everybody knows the post office is faster than clacks... but


Not so sure, I understand they can send pictures now, something about
different colors of light.

Glad you got it working.
--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

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: 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...
6
by: craig.buchinski | last post by:
Ok, i have a Access Database (which is used with my ESRI Arcmap) and i have written a query for a report to pull out our streetlights and group them by billing wattage. The problem is that it is...
7
by: asmian | last post by:
I have a problem query that's not playing nice with my webhost's MySQL server. On the face of it it looks quite innocuous but I need a better way as they've forbidden me to run it any more! Here's...
8
by: chrisdavis | last post by:
Hi All..I have been using Access for about 10 years now but something occurred to me that when I'm trying to GROUP and MAX, it's pulling it for different fields. Example: I have times by hour...
3
by: Shiriah | last post by:
I feel that this is a rather trivial concept that I cannot quite wrap my head around to make work in PHP. I consider myself an intermediate user in SQL (both MSSQL and MySQL) and somewhat novice in...
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...
3
by: kevenj | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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,...

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.