SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict joyce's Avatar
    Join Date
    Aug 2001
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorting acs and desc

    i have select out a list of record, 'name, and 'date'. how can i do it in such a way that when i clicked the title 'name', all the records below will sort by name, asc and when i clicked another time(name), the records below will arrange by desc order. that goes the same with 'date'.

    how can i do that??

  2. #2
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A crude but effective way could be;

    Code:
    switch ( $sort ) {
       case "asc":
        $sql = "SELECT name FROM users ORDER BY name ASC";
        echo ( "<a href=$PHP_SELF?sort=desc>Names</a>" );
      break;
       case "desc":
        $sql = "SELECT name FROM users ORDER BY name DESC";
        echo ( "<a href=$PHP_SELF?sort=asc>Names</a>" );
      break;
       default:
        $sql = "SELECT name FROM users ORDER BY name ASC";
        echo ( "<a href=$PHP_SELF?sort=desc>Names</a>" );
      break;
    }

  3. #3
    SitePoint Addict joyce's Avatar
    Join Date
    Aug 2001
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what if i want it in such a way that, when i clicked name and it's arranging in descending order...an arrow pointing down will appear??

    this is the 3 fields:
    PHP Code:
    print "<tr><td width=30% bgcolor=#9999ff align=center><font face=verdana color=#ffffff size=1>Date</font></td>\n";
        print 
    "<td width=40% bgcolor=#9999ff align=center><font face=verdana color=#ffffff size=1>Name</font></td>\n";
        print 
    "<td width=30% bgcolor=#9999ff align=center><font face=verdana color=#ffffff size=1>Expected Salary</font></td></tr>\n"
    Last edited by joyce; Jan 16, 2002 at 04:30.

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put the appropriate html in the proper case. or have another if/switch later on that'll print html based on the value of $sort
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Exactly. It may be worth creating a function to handle this which you can call as needed;

    Code:
    function sorter ( $sorttype, $sortfield ) {
    switch ( $sorttype ) {
       case "asc":
        $sql = "SELECT * FROM users ORDER BY '$sortfield' ASC";
        $href = "<a href=$PHP_SELF?sort=desc>$sortfield</a>";
        $images = "<img src=images/downarrow.gif>";
      break;
       case "desc":
        $sql = "SELECT * FROM users ORDER BY '$sortfield' DESC";
        $href= "<a href=$PHP_SELF?sort=asc>$sortfield</a>";
        $images = "<img src=images/uparrow.gif>";
      break;
       default:
        $sql = "SELECT * FROM users ORDER BY '$sortfield' ASC";
        $href = "<a href=$PHP_SELF?sort=desc>$sortfield</a>";
        $images = "<img src=images/downarrow.gif>";
      break;
    }
    return array ( $sql, $href, $image );
    }
    But hey - may be I should do some work


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
  •