Sorting Order by both ASC & DESC

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;
}
else
{
    $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


jaggu2003Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

minichickenCommented:
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");
0
jaggu2003Author Commented:
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
0
minichickenCommented:
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.
0
suresh_aspCommented:
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
---------------------------------------------------

if(isset($_GET['sortby']))
{
$result_id=$sql->query("SELECT * FROM ".$database["table_prefix"]."users WHERE is_active=1 ORDER BY $order $sortby");
}
else
{
$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";

regards
suresh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jaggu2003Author Commented:
thank you guys for helping.

Can u guys chk this question;
http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21184380.html

I appreciate your time & inputs
Jaggu Joshi

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.