Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-07-2009, 08:48 PM   PM User | #1
vik1313
New to the CF scene

 
Join Date: Nov 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
vik1313 is an unknown quantity at this point
Post How to display query results under headings derived from another field

Hi all,

Please I'm a newbie in this and have results from a GROUP BY (grouped by col2) query in the format.

col1 | col2
-----------
a1 | a
a2 | a
a3 | a
a4 | a
b1 | b
b2 | b
b3 | b


I wish to display the results in the format
----
a
----
a1
a2
a3
a4
----
b
----
b1
b2
b3

I'll appreciate any help on how to go about this.

Last edited by vik1313; 11-07-2009 at 09:02 PM..
vik1313 is offline   Reply With Quote
Old 11-07-2009, 09:58 PM   PM User | #2
Old Pedant
Senior Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 659 Times in 651 Posts
Old Pedant will become famous soon enoughOld Pedant will become famous soon enough
Display using what? PHP? ASP? Java? C++? What?

The basic answer is the same:
Code:
... get the records ...

priorCol2Value = ""

loop on the records 
{
    if currentCol2Value <> priorCol2Value
    {
        ... output a heading for the new currentCol2Value ...
    }
    priorCol2Value = currentCol2Value
    ... output col1 info ...

    next record
} // end of loop
__________________
"Old age and cunning win out over youth and enthusiasm every time."
Old Pedant is online now   Reply With Quote
Old 11-07-2009, 10:16 PM   PM User | #3
vik1313
New to the CF scene

 
Join Date: Nov 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
vik1313 is an unknown quantity at this point
Post

Quote:
Originally Posted by Old Pedant View Post
Display using what? PHP? ASP? Java? C++? What?

The basic answer is the same:
Code:
... get the records ...

priorCol2Value = ""

loop on the records 
{
    if currentCol2Value <> priorCol2Value
    {
        ... output a heading for the new currentCol2Value ...
    }
    priorCol2Value = currentCol2Value
    ... output col1 info ...

    next record
} // end of loop
Hi Pedant,

Thanks for your reply and sorry for my omission. I'm using PHP and MySQL.
So how do I implement it using PHP.

Cheers!
vik1313 is offline   Reply With Quote
Old 11-08-2009, 12:49 AM   PM User | #4
Old Pedant
Senior Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 659 Times in 651 Posts
Old Pedant will become famous soon enoughOld Pedant will become famous soon enough
I'm not a PHP person, but pretty much what I showed you:

Code:
$priorCol2Value = "";

while( ...there are more records... ) 
{
    $currentCol2value = ...get that column from your PHP record...;
    if ( $currentCol2Value != $priorCol2Value )
    {
        echo "----<br/>" . $currentCol2Value . "<br/>----<br/>\n";
        $priorCol2Value = $currentCol2Value;
    }
    $col1Value = ...get that column from your PHP record...;
    echo $col1Value . "<br/>\n";
}
As I recall, with PHP the code in the while( ... ) *will* get the next record, so that's about all you need.
__________________
"Old age and cunning win out over youth and enthusiasm every time."
Old Pedant is online now   Reply With Quote
Old 11-08-2009, 01:18 PM   PM User | #5
vik1313
New to the CF scene

 
Join Date: Nov 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
vik1313 is an unknown quantity at this point
Post

Quote:
Originally Posted by Old Pedant View Post
I'm not a PHP person, but pretty much what I showed you:

Code:
$priorCol2Value = "";

while( ...there are more records... ) 
{
    $currentCol2value = ...get that column from your PHP record...;
    if ( $currentCol2Value != $priorCol2Value )
    {
        echo "----<br/>" . $currentCol2Value . "<br/>----<br/>\n";
        $priorCol2Value = $currentCol2Value;
    }
    $col1Value = ...get that column from your PHP record...;
    echo $col1Value . "<br/>\n";
}
As I recall, with PHP the code in the while( ... ) *will* get the next record, so that's about all you need.
Hi Pendant,

Thanks for your assistance, I've tried to implement it with the layout above it worked BUT THEN seems to skip the first row in col1.

Outputs:
a
---
a2
a3
a4

b
---
b1
b2
b3

Here's my code:
$priorcol2 = "";
while($row = mysql_fetch_array($result))
{

$currentcol2 = $row['col2'];
if($currentcol2 != $priorcol2)
{
echo $row['col2'];
$priorcol2 = $currentcol2;
}
echo $row['col1'];
}

I guess it's from the loop structure but have not been able to fix it.
vik1313 is offline   Reply With Quote
Old 11-08-2009, 02:29 PM   PM User | #6
vik1313
New to the CF scene

 
Join Date: Nov 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
vik1313 is an unknown quantity at this point
Ok, sorry I've fixed it.

Thanks a lot.
vik1313 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:29 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.