Sorting Order by both ASC & DESC

Posted on 2004-10-28
Last Modified: 2008-03-06
Hi All,

Here am posting one more question on PHP. Am new to this php programming so am using net to develop applications.

I have downloaded code for sort the DB fetched Results. Sorting order is working but its sorting only Ascending order, now I would like to set Desc or Asc.

Please help me to get sort out this problem.

Code is here like this
$default_sort = "username";
if (!isset ($_GET['order']))
    $order = $default_sort;
    $order = $_GET['order'];
$result_id=$sql->query("SELECT * FROM ".$database["table_prefix"]."users WHERE is_active=1 ORDER BY $order");

echo "<b>".$lang["user_list"]["active_users"]."</b><br>".str_replace("<_COUNT_>", $sql->num_rows($result_id), $lang["user_list"]["active_users_count"])."\n";
echo "<table width='100%' border='0' cellpadding='0' cellspacing='1' class='txt'>\n";
echo "  <tr class='tableheader' bgcolor='#cccccc'>\n";
echo "    <td width='15%' bgcolor='#cccccc'><a href=".$general["site_path"]."/admin/user_list.php?order=username>".$lang["menu"]["user"]["login"]["username"]."</a></td>\n";
echo "    <td width='20%' bgcolor='#cccccc'><a href=".$general["site_path"]."/admin/user_list.php?order=Name>".$lang["name"]."-".$lang["LastName"]."</a></td>\n";
echo "    <td width='12%' bgcolor='#cccccc'><a href=".$general["site_path"]."/admin/user_list.php?order=graduate_year>".$lang["my_info"]["graduate_year"]."</a></td>\n";
echo "      <td width='16%' bgcolor='#cccccc'><a href=".$general["site_path"]."/admin/user_list.php?order=auth>".$lang["authentication"]."</a></td>\n";
echo "      <td width='26%' bgcolor='#cccccc'><a href=".$general["site_path"]."/admin/user_list.php?order=last_login>".$lang["last_login"]."</a></td>\n";
echo "      <td width='1%'>&nbsp;</td>\n";
echo "      <td width='1%'>&nbsp;</td>\n";
echo "      <td width='1%'>&nbsp;</td>\n";
echo "  </tr>\n";
echo "  </table>\n";


Thanks for your time & inputs
Jaggu Joshi

Question by:jaggu2003
    LVL 12

    Expert Comment

    for descending: $result_id=$sql->query("SELECT * FROM ".$database["table_prefix"]."users WHERE is_active=1 ORDER BY $order DESC");

    for ascending: $result_id=$sql->query("SELECT * FROM ".$database["table_prefix"]."users WHERE is_active=1 ORDER BY $order");

    or $result_id=$sql->query("SELECT * FROM ".$database["table_prefix"]."users WHERE is_active=1 ORDER BY $order ASC");

    Author Comment

    hey minichiken,

    Its working fine, I have set DESC order for LAST_LOGIN column. but when clicking second time on same link its not changing to ASC order. Its permanently set the order to DESC.

    Any suggestion to change sorting order vice-versa.

    Thanks buddy,
    Jaggu Joshi
    LVL 12

    Assisted Solution

    Ok then it a bit more complicated:

    What you need to do is add this to your link: my_page.php?sort=ASC so it tells the page which order it should sort by.

    Your query:
    $result_id=$sql->query("SELECT * FROM ".$database["table_prefix"]."users WHERE is_active=1 ORDER BY $order $_GET[sort]");

    in your link you will need to do the opposite:

    If $_GET[sort] is ASC then the link so be my_page.php?sort=DESC and vice versa.
    LVL 2

    Accepted Solution

    Remove the following query from your code
    $result_id=$sql->query("SELECT * FROM ".$database["table_prefix"]."users WHERE is_active=1 ORDER BY $order");

    and replace with the following in same place

    $result_id=$sql->query("SELECT * FROM ".$database["table_prefix"]."users WHERE is_active=1 ORDER BY $order $sortby");
    $result_id=$sql->query("SELECT * FROM ".$database["table_prefix"]."users WHERE is_active=1 ORDER BY $order");

    place this line in table where ever you need
    echo "<td width='26%' bgcolor='#cccccc'><a href=".$general["site_path"]."/admin/user_list.php?sortby=DESC>Sort by Descending order</a></td>\n";


    Author Comment

    thank you guys for helping.

    Can u guys chk this question;

    I appreciate your time & inputs
    Jaggu Joshi


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    SQL injection vulnerabilities have been described as one of the most serious threats for Web applications. Web applications that are vulnerable to SQL injection may allow an attacker to gain complete access to their underlying databases. Because…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now