Get all data out of database in reverse order

Discussion in 'PHP' started by fex, Apr 13, 2009.

  1. #1
    Hello,

    I've got a news system on my website which uses a database. When a news post is made, a file is created and the filename is being stored in the database. Then on the page where I want my news to be I include all news files by using all the date in the database.

    The problem is, the oldest post is now on top and I want the newest to be there. So the data must be read in reverse order out of the database or all data must be stored somewhere temporary and being included in reverse order.

    //database connection file
    include_once('plugins/admin/users/dbConfig.php');
    //get all data out of database
    $r = mysql_query("SELECT * FROM News");
    //include all files
    while($row = mysql_fetch_array($r)){
    	include_once("plugins/news/data/" . $row['id'] . ".txt");
    }
    Code (markup):
    Tia
     
    fex, Apr 13, 2009 IP
  2. linkfinders

    linkfinders Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what are the field names in the "News" table
     
    linkfinders, Apr 13, 2009 IP
  3. North Carolina SEO

    North Carolina SEO Well-Known Member

    Messages:
    1,327
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Modify your query statement as:

    SELECT * FROM News order by <fieldname> desc;

    Of course, change the <fieldname> to whatever field you are sorting by without the brackets.


     
    North Carolina SEO, Apr 13, 2009 IP
  4. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    id and title

    title is just to make it easier for me to list all posts so far.
     
    fex, Apr 13, 2009 IP
  5. linkfinders

    linkfinders Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    so..
    SELECT * FROM News order by id desc;
     
    linkfinders, Apr 13, 2009 IP
  6. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Didn't see post of North_Carolina_SEO when posted..

    Thx both ;)
     
    fex, Apr 13, 2009 IP
  7. tariq1654

    tariq1654 Active Member

    Messages:
    291
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #7
    Try this:
    //database connection file
    include_once('plugins/admin/users/dbConfig.php');
    //get all data out of database
    $r = mysql_query("SELECT * FROM News order by id desc");
    //include all files
    while($row = mysql_fetch_array($r)){
    include_once("plugins/news/data/" . $row['id'] . ".txt");
    }
     
    tariq1654, Apr 14, 2009 IP