retriving list of months for "archive"

Discussion in 'PHP' started by mz906, Jan 7, 2009.

  1. #1
    I'm trying to retrive a list of months in a database, but i only want a list of each month and not a list of every accurance, not sure where i'm going wrong, its some logic issue i can't seem to wrap my head around :(

    
    $q = "SELECT created FROM post ORDER BY created DESC;";
    $r = mysql_query($q);	
    print '<ul>';
    while($row = mysql_fetch_assoc($r)){			
        extract($row);
        $year = date("Y", strtotime($created));
        $month = date("F", strtotime($created));			
        print '<li>'.$month.'</li>';			
    }
    print '</ul>';
    
    PHP:
    which results in:
    January
    January
    January
    January
    January
    February
    February
    February
    February
    March
    March

    instead of:
    January
    February
    March
     
    mz906, Jan 7, 2009 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    SELECT MONTHNAME(created) FROM post;
    Code (markup):
     
    javaongsan, Jan 7, 2009 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    You can use the DISTINCT.
    SELECT DISTINCT created FROM post ORDER BY created DESC
    Code (markup):
     
    Kaizoku, Jan 7, 2009 IP