SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    As I site here typing small PHP commands getting exactly the content I want from my database in return, I'm starting to wonder what life was like before PHP.

    However, being just a beginner, I have what I belive must be a pretty basic question.

    Let's say you do a select query like the following and save the results in the $result var. like this:

    Code:
    $result = mysql_query("SELECT ID, Name FROM Category WHERE ID=$catid");
    Then, doing something like the following, the results are printed out:

    Code:
    while ($row = mysql_fetch_array($result)) {
    echo ($row["ID"]);
    echo ($row["Name"]);
    }
    However, the order of the content returned here (in this case the names and IDs of categories) seem to be totally random. So what if I want to sort the output alfabetically after the name of the categories. Or what if I want to sort by the ID number?

    Thanks in advance !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  2. #2
    SitePoint Enthusiast Laura Brandt's Avatar
    Join Date
    Mar 2001
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $result = mysql_query("SELECT ID, Name FROM Category WHERE ID=$catid");

    while ($row = mysql_fetch_array($result)) {
    echo ($row["ID"]);
    echo ($row["Name"]);
    }
    Use this code:

    $result = mysql_query("SELECT ID, Name FROM Category WHERE ID=$catid ORDER BY Name"); -> this code will sort by Name. If you want to sort by ID, change the variable after ORDER BY to ID

    Then you can use the while loop for display.

    Hope this helps
    Aletia Hosting - Full-Featured, Superfast Web Hosting
    PHP4, MySQL, Perl/CGI, Multiple Domain Support and More.
    Plans Start From: 200MB + 10GB Transfer at $9.95/mth
    http://AletiaHosting.com - sales@aletia.net - AIM: AletiaCorp - ICQ: 111160011

  3. #3
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In addition, you change reverse the way it is sorted...like this:

    ORDER BY date DESC

    ...or, it's counterpart:

    ORDER BY date ASC

    Just remember that it goes after any WHERE clauses, but before any LIMIT statements.

  4. #4
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks both of you. One day when I'm a great PHP guru I'll think back upon this and laugh.
    Sincerely,

    Eirik Johansen
    Netmaking AS


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
  •