Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Junior Member Tonic's Avatar
    Join Date
    Jan 2004
    Posts
    25
    Member #
    4684
    hi there, I've written a PHP script to retrieve news entries from my mySQL database but I get the information accending, instead of decending... how do i change it so that the news articles display newer on top? (Highest news ID to lowest)

    this is what I have:

    Code:
    mysql_select_db("news") or die(mysql_error()); 
    
    $query  = "SELECT * FROM news_post";
    $result = mysql_query($query);
    
    while($row = mysql_fetch_assoc($result))
    {
        echo "<div style='border: 1px solid rgb(170, 170, 170); padding: 10px; margin-bottom: 10px; clear: both;'>
    <div style='margin: 5px; padding: 5px; float: right;'>
    <img src='images/cs_source.gif' style='border: 0pt none ;'>
    </div><div class='topictitle'> 
    	 " . "{$row['news_title']} </div><div class='postdetails'>" .
             "{$row['news_date']} by " .
             "{$row['news_poster']} </div><hr><div class='postbody'>" .
             "{$row['news_body']} </div><br></div>";
    }
    I know I've got my HTML and PHP code totally mixed, but that's just the general idea of how I got it set up. Any ideas?

  2.  

  3. #2
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,656
    Member #
    1234
    Liked
    137 times
    $query = "SELECT * FROM news_post ORDER BY news_ID DESC"

    ASC ascends the table based on the ORDER BY value, DESC descends the table.

    Unless you're always going to need the whole table, it's best if you LIMIT the query by a limiting value.

    http://www.w3schools.com/sql/default.asp

    Read that, it'll help.
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  4. #3
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    It bears mentioning, by the way, that your id column isn't necessarily the most reliable way to order your information. If you have a date (say, news_date? :-P) and that row is of type `date' in the database, you can have the query order by that, too, and it'll order by the date itself, which is probably best and makes the most sense. If your dates are of type `text' or something like that, however, ordering by id is the way to go.

  5. #4
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Shadowfiend is correct. An ID is only a means of uniquely identifying one row. It is only coincidence that you can usually sort by it. However, it is possible to have rows with IDs 1 and 3 that were created after ID 2, for example. If you need to sort, add some sort of timestamp column.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  6. #5
    Junior Member Tonic's Avatar
    Join Date
    Jan 2004
    Posts
    25
    Member #
    4684
    alright, thanks for the help guys. I do have a time stamp appointed to each, so i'll do it that way

  7. #6
    Junior Member Tonic's Avatar
    Join Date
    Jan 2004
    Posts
    25
    Member #
    4684
    my bad, forgot to ask something else that you brought up - now that I got the news posts ordering correctly, how would I limit news posts to say, the current month?

  8. #7
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Ah, now that would be slightly more difficult. Is your timestamp a date or an actual timestamp number? Whichever of the two it is, you want to limit your query using a `WHERE' clause, namely:
    Code:
    SELECT * FROM news_post WHERE news_date > ****
    Where in ****, you can put either a date (if it's a date type) or a timestamp number (if it's a timestamp). Read the link Wired posted, you'll find out about a good bit of what's possible using SQL (hint: it's a lot :-)).


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:13 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com