Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    64
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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

    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
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    When you say the column has a list of months, does that mean one row contains a value "Jan, Feb, Jan" or does that mean you have several rows, each containing one month?

  • #3
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    64
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I apologize. There are several rows, each containing a month:
    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

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Since you are using PHP already you can simply ORDER BY month, day and then echo the results. You'll at first get this:

    Code:
    Jan 1
    Jan 2
    Jan 7
    Jan 30
    Feb 5
    Feb 12
    Feb 14
    Feb 16
    That can be turned into your desired output by only printing the month when it changes. Example:

    PHP Code:
    $previousMonth "";

    for (
    $i 0mysql_num_rows($result); $i++) {
        if (
    $previousMonth != $rows['month']) {
            echo 
    $rows['month']."<br />";
            
    $previousMonth $rows['month'];
        }
        echo 
    $rows['day']."<br />";

    I have to wonder why you don't just have a single column of data type "date", which would store your month and day in one column.

  • #5
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    64
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks,
    The reason for not having the date is that I was trying to understand the answer and wanted an easy way to ask the question. The actual information is Projects and PDF files where multiple PDF files (dates) are associated with the same Project (Month).

    I understand what you're trying to do, but I think there is a problem. I'm not sure if it's the code you gave me or something I did, but when I open the page, it is "thinking" indefinatly and never opens. I'll look over my code some more unless you see something that was omitted from your code? We're both in Utah, I should just come and find you with my laptop

    Thanks for your help, I'll search what I have for mistakes.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Whoops yeah don't assume I've tested and debugged my sample code. The "for" loop condition is wrong, should be $i < mysql_num_rows($result) and inside the loop I forgot to include a call to mysql_fetch_array().

  • #7
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    64
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks,
    I've been working on this for the past few hours. My biggest problem.....I was using group by instead of order by. Thanks for all the help!

    PHP Code:
    <?php

    $previousMonth 
    ""
    for (
    $i 0$i mysql_num_rows($result); $i++) { 
    while (
    $rows mysql_fetch_array($result)) {

        if (
    $previousMonth != $rows['month']) { 
            echo 
    $rows['month']."<br />"
            
    $previousMonth $rows['month']; 
        } 
        echo 
    $rows['day']."<br />"
    }  
    }
    ?>
    Last edited by Local Hero; 04-25-2009 at 02:37 AM.

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The "for" loop is useless if you use a "while" loop too. Just take out the "for" loop.


  •  

    Posting Permissions

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