Multiple identical values - only want to output once

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marco
    Senior Member
    • Nov 2000
    • 827
    • 3.8.x

    Multiple identical values - only want to output once

    Howdy,

    I am currently in the process of writing my own news system in PHP/MySQL. I am currently trying to write a monthly archive system, but it isn't quite working yet. That's where you guys come in.

    All the news will be stored in a table, called "news", with the following fields:

    - id
    - postdate
    - title
    - newstext

    The "postdate" field contains a timestamp, formatted like this: 20040409163002. Now what I'm trying to do is build a list with links to the monthly archives (where archived news means: news older than 45 days). Right now, I have this:


    PHP Code:
    function build_archive_list($days)
       {
           
    $query mysql_query("
               SELECT postdate,
               DATE_FORMAT(postdate, '%M') as month,
               DATE_FORMAT(postdate, '%Y') as year
               FROM news WHERE DATE_SUB(NOW(), INTERVAL 
    $days DAY) >= postdate
               ORDER BY postdate DESC
           "
    ) or die ("Failed to get news dates for archive.");
       
           while (
    $months mysql_fetch_assoc($query))
           {
               
    $month $months['month'];
               
    $year $months['year'];
             echo 
    "<span class=\"row\">" $month " " $year "</span><br />";
           }
       } 
    Now, while this does work, the problem is that when for example I have three news items in February and one in January, it outputs:

    February 2004
    February 2004
    February 2004
    January 2004

    While I really want it to output this:

    February 2004
    January 2004

    So, how can I get the code to output every month only once?
    I've already asked this over at the DevShed forums but no one there really seemed to know. Hopefully someone here will be able to help me out

    Edit -- I realize MySQL's GORUP_CONCAT might be what I'm looking for, but that's in MySQL 4.1, and my host uses 4.0.18 so I'm afraid that's of no use to me.
    Last edited by Marco; Tue 13 Apr '04, 7:39am.
  • Lats
    Senior Member
    • Mar 2002
    • 3671

    #2
    This is what I use...
    PHP Code:
     $query "
      SELECT DISTINCT DATE_FORMAT(event_start,\"%b %Y\")
       AS 
        starter
       FROM
        company_news
       WHERE
        event_start < '
    $todays_date'
       ORDER BY 
        event_start
        "

    Lats...

    Comment

    • Marco
      Senior Member
      • Nov 2000
      • 827
      • 3.8.x

      #3
      Wow, it works!

      Thanks a ton.

      Comment

      widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
      Working...