Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 9 of 9

Thread: [php][mysql]Display Highest news id

  1. #1

    [php][mysql]Display Highest news id

    I have a php/mysql news management system, and on my main page i want to display my latest news. Basically i need to include the page with the highest news id my pages are set like this:

    "news.php?newsid=21"

    Is there a way to do this?
    check out my band at:
    www.maive.co.nr or www.myspace.com/maivemusic

    Demo Out Soon!!

  2. #2
    you need your query to be something like:
    Code:
    $result = mysql_query( "SELECT * FROM newstable LIMIT limitnum ORDER BY newsid ASC" )'
    without knowing what your tables or variables are called, that's my best guess. basically you just need to ORDER BY

  3. #3
    602
    posts
    Backend Specialist
    if you want the highest id's first, you would use
    Code:
    SELECT * FROM newstable ORDER BY newsid DESC LIMIT limitnum
    (also the limit must come after order by)

    hth

  4. #4
    I only want to show the highest news id, there will be links to the others...
    check out my band at:
    www.maive.co.nr or www.myspace.com/maivemusic

    Demo Out Soon!!

  5. #5
    then LIMIT 1

    @skoob: word. i couldn't remember the syntax off hand.

  6. #6
    Would it be ordered by DESC? To get the latest news?
    Member #2 of the "I wont critique Timmytot's designs anymore" club.

  7. #7
    602
    posts
    Backend Specialist
    you should really have a datetime field on the news entry. like "date_added" or something, and use that for ordering. using an auto_increment ID could work...but it won't work if for some reason you insert an ID manually to a lower available news ID.

    @ sebs - Since the news ID will be 1 more than the previous news story, it will have a greater number, and you want to sort from highest to lowest to get the most recent news story added.

  8. #8
    Quote Originally Posted by skOOb
    you should really have a datetime field on the news entry. like "date_added" or something, and use that for ordering. using an auto_increment ID could work...but it won't work if for some reason you insert an ID manually to a lower available news ID.

    @ sebs - Since the news ID will be 1 more than the previous news story, it will have a greater number, and you want to sort from highest to lowest to get the most recent news story added.
    Yes skoob that would be DESC and type in number going doen

    1
    2
    3
    4

    and sort by DESC 4 would be at the top, that woudl be the newest story.
    Member #2 of the "I wont critique Timmytot's designs anymore" club.

  9. #9
    try this sql statement:

    $result=mysql_query("select max(id) as maxID from tbltable");
    $row=mysql_fetch_object($result);

    echo $row->maxID;

    this would output the maximum id number in the database

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012