Announcement

Collapse
No announcement yet.

sorting acs and desc

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Are the records stored in a database??

    Comment


    • #3
      your mysql query should look something like..

      SELECT fields FROM table ORDER BY asc

      Comment


      • #4
        snippets from working code:
        PHP Code:


        // allow users to sort our results
        $i=0;
        $sort[++$i] = ($orderBy == "field_one ASC") ? "field_one DESC" "field_one ASC";
        $sort[++$i] = ($orderBy == "field_two ASC") ? "field_two DESC" "field_two ASC";
        $sort[++$i] = ($orderBy == "field_three ASC") ? "field_three DESC" "field_three ASC";

        $url "<a href=\"your_file.php3?orderBy=";

        $i=0;
        $sort[++$i] = $url $sort[$i] . "\">Field 1</a>";
        $sort[++$i] = $url $sort[$i] . "\">Field 2</a>";
        $sort[++$i] = $url $sort[$i] . "\">Field 3</a>";

        echo 
        "<table width='100%' cellpadding='0' cellspacing='0' border='0'>\n";
        echo 
        " <tr>";
        echo 
        "  <th>$sort[1]</th>\n";
        echo 
        "  <th>$sort[2]</th>\n";
        echo 
        "  <th>$sort[3]</th>\n";
        echo 
        " </tr>\n";

        // Build the query
        $query "SELECT field_one, field_two, field_three FROM table_name ORDER BY $orderBy"
        You'll want to add a check for $orderBy to make sure it's valid, and to give it a default value if if fails the check.

        Comment

        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
        Working...
        X