SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    The Jellophonic Autobrain CHeeSeBLiND's Avatar
    Join Date
    Jul 2001
    Location
    Bolton, England
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    newest entries at top

    i am currently working on a guestbook script and i was just wondering how i can get the newest entries to appear at the top of the page. Thx alot

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you add ORDER BY id DESC to your SQL query it should work.

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I am understanding you correctly Anarchos you are recommend using the ID of each post for sorting. I would not recommend using an ID because using auto_increment sometimes gives you wacky results. You are better stoting a timestamp in the database and using that. You get a timestamp by doing this:

    PHP Code:
    $TimeStamp time(); 
    Then use:

    ORDER BY TimeStamp Desc

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    SitePoint Member
    Join Date
    Jul 2001
    Location
    WDSM, IA
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the ID to "order by ID desc" should work fine. If by "wacky results" you mean the records are displayed "out of order" within the database, I know what you're talking about. However, once you pull the records with a query in a particular order (in this case, ID/descending), it displays properly - it's still going to display the greater ID number first...

    I used the same technique Anarchos describes to display the most recent headline first on my website's front page: Troop Z Headquarters

    Last edited by Yawmark; Jul 31, 2001 at 08:11.
    Yawmark

    There are 10 kinds of people...
    Those who understand binary, and those who don't.

  5. #5
    SitePoint Guru
    Join Date
    Apr 2001
    Location
    BC, Canada
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah.. but the id might get screwy after u delete and add some entries

    say u have

    id
    1
    2
    3
    4
    5

    then the next day for some reasson u delete id 3 because someone put a rude entry into the guest book
    now u have

    id
    1
    2
    4
    5

    no big deal right? RIGHT! Now you have to be careful on how you insert data base. You might end up giving the new entry the id of 3, because it would be unique.

    u again would have
    id
    1
    2
    3
    4
    5

    but id-3 would actually be the newest

    i would defintly agree with seanf. Best idea is to use time();
    that way you can also display the date when it was entered and other useful time related information

  6. #6
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, MySQL will always add ids at the end. It won't reassign to deleted ids.
    ck :: bringing chris to the masses.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Wolverhampton, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, on primary key fields, the newest entry is always added with the id bein' 1 more than the previous highest id... (unless u instruct it to have an id that not already taken???)

    beachball

  8. #8
    SitePoint Member
    Join Date
    Jul 2001
    Location
    WDSM, IA
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once again, I second Anarchos (and beachball). The technique they describe ("order by ID desc") works - I'm using it as we speak. There's no need to add an extra field...

    Yawmark

    There are 10 kinds of people...
    Those who understand binary, and those who don't.

  9. #9
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just because something works it does not mean it is the best way!

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  10. #10
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using the id field should work perfect. it IS the best way. if you get "weird results" then something's definitely wrong. the newest entries will ALWAYS have the highest id.


    Originally posted by beachball
    yeah, on primary key fields, the newest entry is always added with the id bein' 1 more than the previous highest id...
    actually you mean on AUTO_INCREMENT fields. and the AUTO_INCREMENT col doesn't need to be the PRIMARY KEY. UNIQUE will do. but in this case, yeah, he would have that as the PRI KEY, but just clearing that up.

    in MySQL 3.23 the AUTO_INCREMENT number will ALWAYS be one higher than the last. in 3.22 however, if the most recent one is deleted it will be reused again.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  11. #11
    SitePoint Member
    Join Date
    Jul 2001
    Location
    WDSM, IA
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just because something works it does not mean it is the best way!
    Of course not, but there's been no evidence presented to lead me to believe that adding an extra timestamp field is any "better" than simply using an auto-incremented ID in this circumstance. I haven't had any "wacky results" when using this method, and I fail to see how timestamping provides any benefit at all. By all means, explain why timestamping is more efficient or reliable in this circumstance and I'll use it happily!
    Yawmark

    There are 10 kinds of people...
    Those who understand binary, and those who don't.


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
  •