Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005
    Posts
    15

    Unanswered: 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.

  2. #2
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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>" );
    ?>

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •