If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Seperating Groups - almost there

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-24-09, 14:21
localhero localhero is offline
Registered User
 
Join Date: Jul 2005
Posts: 15
Seperating Groups - almost there

Lets say I have a table with 1 column having a list of months:
Jan, Jan, Feb, Jan, Feb, Feb, Jan, Feb
And another column having a list of dates associated with those months:
1,2,5,7,14,12,30,16
It looks like this:
Jan | 1
Jan | 2
Feb | 5
Jan | 7
Feb | 14
Feb | 12
Jan | 30
Feb | 16

I want to view the results like this:
Jan
1
2
7
30

Feb
5
14
12
16

Here is what I'm working with:
PHP Code:
<?php
$sql
="SELECT month, day FROM calendar GROUP BY month";
$sql2="SELECT month, day FROM calendar ORDER BY month";
$result=mysql_query($sql);
$result2=mysql_query($sql2);
while(
$rows=mysql_fetch_array($result)){
?>
<table><tr><td>
<?php echo( $rows['month'] ); ?>
</td></tr><tr><td>
<?php
while($rows2=mysql_fetch_array($result2)){
echo 
$rows2['day'] ;?>

 }
?>
</td></tr></table>
<?php
 
}
?>
I'm getting 2 tables - Jan & Feb, but all the dates go in Jan. I can see why it's doing that. I can't figure out how to get the Feb dates over in the Feb table. Thanks for any suggestions or direction.
Reply With Quote
  #2 (permalink)  
Old 04-25-09, 09:34
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I didn't try it but the following might be close:
Code:
<?php
   print( "<table>" );
   $current_month="";

   $sql="SELECT month, day FROM calendar ORDER BY month,day";
   $result=mysql_query($sql);

   while( list( $month,$day ) = mysql_fetch_row($result) ) {
        if ( $month != $current_month ) {
              print( "<tr><td>$month</td></tr>" );
              $current_month=$month;
        }
        print( "<tr><td>$day</td></tr>" );
   }

   print( "</table>" );
?>
Reply With Quote
  #3 (permalink)  
Old 04-25-09, 09:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT month
     , day 
  FROM calendar
ORDER
    BY FIND_IN_SET(month
          ,'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec')
     , day
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On