Print Page | Close Window

Another easy one... SQL & ORDER BY

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=4292
Printed Date: 30 March 2026 at 3:55am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Another easy one... SQL & ORDER BY
Posted By: KCWebMonkey
Subject: Another easy one... SQL & ORDER BY
Date Posted: 16 July 2003 at 2:27pm

i have a page that displays the records in a table from a db. How do i go about making a link that when clicked, will sort the data displayed by a certain column, in Ascending and Descending order.

If no one wants to answer this, can someone at least point me to a tutorial?

Thanks.




Replies:
Posted By: michael
Date Posted: 16 July 2003 at 7:27pm

It is fairly easy to do.
<%
Dim strSortOrder
strSortOrder = Request.QueryString("so")
If strSortOrder = "" THEN
 strSortOrder = "asc"
END IF
'Your connection suff
strSQL = "Select * from yourtable order by yourfiled " & strSortOrder
'Execution Stuff
.......
If strSortOrder = "asc" THEN
Response.Write "<td><a href=""thispage.asp?so=desc"">Column to be sorted by</a></td>"
ELSE
Response.Write "<td><a href=""thispage.asp?so=asc"">Column to be sorted by</a></td>"
END IF
....


That should give you an idea. I use something similar but use sessions to remeber the sort order, is up to you.



-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: KCWebMonkey
Date Posted: 17 July 2003 at 6:45am
thanks michael, i thought it was something similar to that, but i couldn't figure it out.


Posted By: ljamal
Date Posted: 17 July 2003 at 8:41am
I'd change this:
If strSortOrder = "" THEN
strSortOrder = "asc"
END IF
to

If not strSortOrder = "desc" THEN
strSortOrder = "asc"
END IF

That way no one could change your sql statement by adding a sql statement to the querystring (sql injection)


-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming


Posted By: michael
Date Posted: 17 July 2003 at 9:21am
true, better that way.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net