SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Sorting news

  1. #1
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting news

    Hi, I'm having a problem with sorting. I'm trying to figure out how I can display this news in the opposite order on my site. Can anyone please tell me how? The way it is now each new news post gets displayed underneath the one before but I need them to be displayed above the one before. Can anyone please tell me how?

    Thanks, Kilroy

    PHP Code:
        // Query the database for news
        
    $result $db->query('SELECT * FROM news');

        
    // Display the news
        
    while ($row mysql_fetch_array($resultMYSQL_BOTH)) {
        echo 
    '<div class="content"><div class="newsheader"><img src="forum/style_images/1/nav_m.gif" alt="Arrow" /> ';
        echo 
    $row['NewsTitle'];
        echo 
    '</div><p>';
        echo 
    $row['NewsContent'];
        echo 
    '</p><div class="newsfooter">( ';
        echo 
    $row['NewsDate'];
        echo 
    ' )</div></div>';
        } 

  2. #2
    SitePoint Addict streetlife's Avatar
    Join Date
    Mar 2004
    Location
    D-Town
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kilroy, if you have a date/time field in your table (if you don't then add one!) then you can use SQL to order the results either ascending or descending, like follows:
    PHP Code:
    $result $db->query('SELECT * FROM news ORDER BY date ASC'); 

  3. #3
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a date field in my table, yes . Thanks for the quick response, trying it out now

  4. #4
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what worked:

    PHP Code:
        $result $db->query('SELECT * FROM news ORDER BY NewsDate DESC'); 
    Thank you so much

  5. #5
    SitePoint Addict streetlife's Avatar
    Join Date
    Mar 2004
    Location
    D-Town
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good, I think I read you might need to change the 'ASC' for 'DESC' though, my bad.

  6. #6
    SitePoint Addict streetlife's Avatar
    Join Date
    Mar 2004
    Location
    D-Town
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, there you go!


Bookmarks

Posting Permissions

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