Results 1 to 3 of 3

Thread: Ascending vs. Decending Order

  1. #1
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default Ascending vs. Decending Order

    Hey all...

    I have a PHP page querying a MySQL database for a list of articles (and related information).

    Here is that PHP:

    PHP Code:
    <?
                        $id
    =$_REQUEST['id'];
                        
    $sql="select * from fiction where id=$id";
                        
    $result=mysql_query($sql);
                        while(
    $rs=mysql_fetch_array($result))
                        {
                        
    ?>
                        
                        <div class="featured">
                            <span class="h7"><?=$rs['title'];?></span><br />
                            <img src="cms/upload/<?=$rs['image'];?>"><br />
                            <span class="h8"><?=$rs['content'];?></span>
                        </div>
                                            
                        <?
                        
    }
                        
    ?>
    It works fine...but not exactly how I'd like it to.
    It displays the entries (made through a backend) in the order they were entered (i.e. the newest entry is at the bottom). I'd like to reverse this so the newest entry is at the top.

    I know I have to change the SQL query but don't know enough PHP to know how to. Can someone help me out here?

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

    Default

    Code:
    $sql="select * from fiction where id=$id order by id desc";
    so long as the ids are auto incremented, this will return the newest registered first because their id will be greater

    and if you wanted to limit the results per page you could apply pagination techniques.

    Code:
    $sql="select * from fiction where id=$id limit 10 order by id desc";

  3. #3
    Join Date
    Aug 2007
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Since your existing query says:
    select * from fiction where id=$id
    I assume that the 'id' field relates to some kind of category and that all the records returned have the same 'id' value.. if this is the case then boogyman's suggestion wont work for you however his method is correct.

    Do you have an auto-incrementing primary key field for the 'fiction' table? If you do, then use boogyman's suggestion but rather than use the ID field in the order by clause, use the primary key of the fiction table.

    If you do not have an auto-incrementing primary key already, I believe that if you create one, they records 'should' be assigned a key in the order of creation.

    Alternatively you can add a timestamp or datetime column to your fiction table which you can use to store the date-created. I believe timestamp columns are automatically populated on creation of the record, so this could be handy in your case.

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
  •