Search Tools Links Login

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


This is the simplest/fastest code that shows how to implement multiple-column sorting (Up & Down) in ASP.
Excellent for ASP Database Begineers

Original Author: Sine45

Code


<%
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
else
thissort = sort
end if
sql = sql & " ORDER BY " & thissort
end if
Response.Write "

ORDER BY: " & thissort & "

"
Response.Write "<a href=""" & url & """>Reset Order"
'Response.End
set rs = conn.Execute(SQL)
'print headers
Response.Write ""
for i=0 to rs.fields.count - 1
fname = rs.fields(i).name
Response.Write ""
next
Response.Write ""
'print recs
do while not rs.eof
Response.Write ""
for i=0 to rs.fields.count - 1
Response.Write ""
next
Response.Write ""
rs.movenext
loop
Response.Write "
<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 "
" & rs(i) & "
"
rs.close
conn.Close
set rs = nothing
set conn = nothing
%>

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 97 times

Categories

ASP/ HTML

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.