[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Sorting Order by both ASC & DESC

Posted on 2004-10-28
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 12

Expert Comment

ID: 12432256
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

ID: 12432315
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

minichicken earned 75 total points
ID: 12432482
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.

Accepted Solution

suresh_asp earned 75 total points
ID: 12432505
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

ID: 12432820
thank you guys for helping.

Can u guys chk this question;

I appreciate your time & inputs
Jaggu Joshi


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

650 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