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

x
?
Solved

Sorting Order by both ASC & DESC

Posted on 2004-10-28
5
Medium Priority
?
511 Views
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;
}
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
Comment
Question by:jaggu2003
[X]
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
5 Comments
 
LVL 12

Expert Comment

by:minichicken
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");
0
 

Author Comment

by:jaggu2003
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
0
 
LVL 12

Assisted Solution

by:minichicken
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.
0
 
LVL 2

Accepted Solution

by:
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
---------------------------------------------------

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
 

Author Comment

by:jaggu2003
ID: 12432820
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: 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