VB icon

Simplest Multiple - Column sorting (Up & Down) in ASP

Submitted on: 1/6/2015 8:05:00 PM
By: Sine45 (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 1052
     This is the simplest/fastest code that shows how to implement multiple-column sorting (Up & Down) in ASP. Excellent for ASP Database Begineers

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
' Name: Simplest Multiple - Column sorting (Up & Down) in ASP
' Description:This is the simplest/fastest code that shows how to implement multiple-column sorting (Up & Down) in ASP.
Excellent for ASP Database Begineers
' By: Sine45 (from psc cd)

dim conn, connString
connString = "nwind"
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open connstring 
dim rs, sql, url,fname,sort,lastsort,thissort
url = Request.ServerVariables("URL")
sql = "SELECT CompanyName, ContactName, Address, City, Phone From Customers"
sort = lcase(request("sort"))
lastsort = lcase(request("lastsort"))
if sort<>"" then
	if lastsort=sort then
		thissort = sort & " desc"
	elseif instr(lastsort,sort & " desc") then
		thissort = replace(lastsort,sort & " desc",sort)
	elseif instr(lastsort,sort) then
		thissort = replace(lastsort,sort,sort & " desc")
	elseif lastsort<>"" then
		thissort = lastsort & "," & sort
		thissort = sort
	end if
	sql = sql & " ORDER BY " & thissort
end if
Response.Write "<p><b><font color=blue>ORDER BY</font>:</b> " & thissort & "</p>"
Response.Write "<a href=""" & url & """>Reset Order</a>"
set rs = conn.Execute(SQL)
'print headers
Response.Write "<table border=1><tr>"
for i=0 to rs.fields.count - 1
	fname = rs.fields(i).name
	Response.Write "<th><a href=""" & url & "?sort="& fname &"&lastsort="& thissort & """>" & fname 
	if instr(thissort,lcase(fname & " desc")) then
		Response.Write " -"
	elseif instr(thissort,lcase(fname)) then
		Response.Write " +"	
	end if
	Response.Write "</th>"
Response.Write "</tr>"
'print recs
do while not rs.eof
	Response.Write "<tr>"
	for i=0 to rs.fields.count - 1
		Response.Write "<td>" & rs(i) & "</td>"
	Response.Write "</tr>"
Response.Write "</table>"
set rs = nothing
set conn = nothing

Other 7 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.