SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Sorting Data

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    United Kingdom
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting Data

    I've got a list of inde that is updated automatically when new articles are submitted. However, they update with the most recent at the bottom, the oldest at the top. I'd like it to be the other way round.

    I'd also like to know, if possible a way of giving the redaer options at the top of the page to display them either most recent, by alphabetical order, or by most popular.

    Thanks for any help
    No one is a virgin, life screws us all

  2. #2
    SitePoint Member
    Join Date
    Jun 2001
    Location
    Antwerp
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do sorting with DESC and ASC, like this:

    Code:
     
    SELECT ID,PlayerName,Score,ScoreDate FROM scores ORDER BY Score DESC
    This gets hiscores out of a mysql table and sorts them descending (highest first) by the score field. The 'ORDER BY Score DESC' bit takes care of that. 'ORDER BY Score ASC' would sort ascending, so the lowest score would be first. You could also sort by other column names, like scoredate or playername.

    I haven't got a lot of experience with PHP-MySQL, but it'd be fairly straightforward to create something where the user can define the sorting by modifying some PHP var.

    By the way, the entire code from which i extracted the sample above is this:

    Code:
    <?
       $dbcnx = mysql_connect("somedb.server.com", "mylogin", "mYpAssW0rd");
       mysql_select_db("mydb", $dbcnx);
       $result = mysql_query("SELECT ID,PlayerName,Score,ScoreDate FROM scores ORDER BY Score DESC");
       $i = 1;
       while ( $row = mysql_fetch_array($result) ) {
                echo("loaded=true&player".$i."=".$row["PlayerName"]."&score".$i."=".$row["Score"]."&scoredate".$i."=".$row["ScoreDate"]."&");
                $i = $i + 1;
       }
    ?>
    This is used for retrieving hiscores for a Flash game i made. You can find it at http://www.katdesign.be/memory.php3
    katdesign says 'meow'

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    United Kingdom
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not quite sure how to implement that into my code, whilst keeping the links. I need to sort the links to the articles with most recent first. Here's the codew I'm using:

    <?php

    // Connect to the database server
    $dbcnx = @mysql_connect("localhost",
    "myuser", "mypassword");
    if (!$dbcnx) {
    echo( "<P>Unable to connect to the " .
    "database server at this time.</P>" );
    exit();
    }

    // Select the gamecube database
    if (! @mysql_select_db("dbnumber") ) {
    echo( "<P>Unable to locate the site " .
    "database at this time.</P>" );
    exit();
    }

    // Request the previews
    $result = mysql_query(
    "Select * from gbapreviews");
    if (!$result) {
    echo("<P>Error performing query: " .
    mysql_error() . "</P>");
    exit();
    }

    while ( $row = mysql_fetch_array($result) ) {
    echo("<a href = 'previews.php?id=$row[id]'>$row[Gamename]</a> $row[DatePosted] $row[Genre]" . "<br>");
    }

    ?>
    No one is a virgin, life screws us all

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change:

    $result = mysql_query( "Select * from gbapreviews");

    to:

    $result = mysql_query( "Select * from gbapreviews ORDER BY DatePosted DESC");

    If you need further assistance, then it is best that you also post a description of table gbapreviews - the column names and their data types.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    United Kingdom
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    Thanks, that's just what I wanted
    No one is a virgin, life screws us all


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
  •