Results 1 to 7 of 7

Thread: Show Newest Entry First, Pulling From Database.

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Show Newest Entry First, Pulling From Database.

    I have a page that displays all blog posts for a certain site. This is what the code looks like:

    PHP Code:
    <?php
    $query 
    "SELECT Id,Date,Title,Post FROM blogs";
    $result mysql_query($query);
    while(
    $row mysql_fetch_assoc($result)){ ?>
            <h2><a href="index.php?page=blogs&id=<?=$row['Id']?>" class="ta"><?=$row['Title']?></a> <i><?=$row['Date']?></i></h2>
            <?=$row['Post']?>
            <div class="lorum"></div>
    <?php }
    Is there any way that I can display those posts shown at the bottom, at the top? I'd like the newest entries to be shown first, before the older. Any ideas on how to flip it to make that possible?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Code:
    $query = "SELECT Id,Date,Title,Post FROM blogs ORDER BY `Id` DESC";
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Is there someway I can just ORDER BY DESC without using the Id? The Id is not based on order. The Id is dynamically created this way:

    PHP Code:
    $id date("mdYHis"); 
    So, the ORDER BY `Id` would only work for a year...then once the next year started, it wouldn't work anymore.
    Any ideas?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    In order for it to order the results desc, you would need something to go off of (such as an auto-incrementing id). Perhaps you could add a column in your db called "order" and have it auto-increment or something.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by alexjewell View Post
    Is there someway I can just ORDER BY DESC without using the Id? The Id is not based on order. The Id is dynamically created this way:

    PHP Code:
    $id date("mdYHis"); 
    So, the ORDER BY `Id` would only work for a year...then once the next year started, it wouldn't work anymore.
    Any ideas?
    that would still work, the date function in php is generated based upon time(), and time() counts up so while its not the usual auto-incrementing field, it would still work.

  6. #6
    Join Date
    May 2006
    Location
    New York City
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by boogyman View Post
    that would still work, the date function in php is generated based upon time(), and time() counts up so while its not the usual auto-incrementing field, it would still work.
    it would work if it were date(YmdHis) because then the year would be first, but since he has it with the month first, date("mdYHis"), it would not. For example, look at this:

    dec 27, 2007 7:30:00 = 12272007073000
    jan 27, 2008 7:30:00 = 01272008073000

    a php or sql sort engine would list jan 27, 2008 first even though it's a later date.

    php.net documentation for mysql_data_seek() offers this solution:
    Code:
    for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
        if (!mysql_data_seek($result, $i)) {
            echo "Cannot seek to row $i: " . mysql_error() . "\n";
            continue;
        }
    
        if (!($row = mysql_fetch_assoc($result))) {
            continue;
        }
            <h2><a href="index.php?page=blogs&id=<?=$row['Id']?>" class="ta"><?=$row['Title']?></a> <i><?=$row['Date']?></i></h2>
            <?=$row['Post']?>
            <div class="lorum"></div>
    }
    Last edited by kosi; 01-07-2008 at 09:48 PM.

  7. #7
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Thank you Kosi - so the code you offered above would work without me having to change the way I'm saving blog posts in the database?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

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
  •