[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

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


0
jaggu2003
Asked:
jaggu2003
  • 2
  • 2
2 Solutions
 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now