Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting by clicking on header in a table 3

Status
Not open for further replies.

TonyU

Technical User
Feb 14, 2001
1,317
US

Hi all
I would like to know how can I sort my recordset by simply clicking on the table header.
I'm populating a table from recordset1 and would like to give the user the ability to sort it on the fly. Thanks in advance. Tony
:cool:
 
you can do this by simply creating a form inside the table with the first row of the table as checkboxes, each check box can have a diferent value.
then the visitor clicks on submit or sort, whichever you call it, and you can have a simple script like

<%
dim choice
choice = request.form(&quot;chosen&quot;)
if choice = &quot;1&quot; then variable = &quot;autonumber&quot;
if choice = &quot;2&quot; then variable = &quot;accountnumber&quot;
etc etc
if choice = &quot;&quot; then variable = &quot;autonumber&quot;
%>

then on your query use

<%
query = &quot;SELECT * FROM table SORT BY &quot; & variable & &quot; DESC&quot;
%>

this will then depending on what is selected from the first part, pull the records from the database in the order needed

hope that is what you wanted.
 
Will need to store the current sort order in a session variable, too, since most sites that offer that functionality allow it to sort one way by clicking once, and then the other by clicking again.

Compare to see if
(a) user clicked the same thing
(b) what current sort order is, and flip it if it is the same

:)
paul
penny.gif
penny.gif
 
or you can make each header a link to the same page with the sort fields in the querystring:

<%
Dim sortOrder
Dim sortField
Dim sSQL

sortField=Request.QueryString(&quot;s&quot;)
If sortField = &quot;&quot; Then
sortField = &quot;Name&quot; 'default to sort by name
End If
sortOrder=Request.QueryString(&quot;o&quot;)
If sortOrder = &quot;&quot; Then
sortOrder = &quot;ASC&quot; ' default to ascending
End If

sSQL = &quot;SELECT * FROM tablename WHERE field='whatever' ORDER BY &quot; & sortField & &quot; &quot; & sortOrder
..... continue db processing.....


'swap sort order
If sortOrder = &quot;ASC&quot; Then
sortOrder = &quot;DESC&quot;
Else
sortOrder = &quot;ASC&quot;
End If


'page headers...
%>
<a href=&quot;thisPage.asp?s=Name&o=<%=sortOrder%>&quot;>Name</a> <a href=&quot;thisPage.asp?s=State&o=<%=sortOrder%>&quot;>State</a> <a href=&quot;thisPage.asp?s=Date&o=<%=sortOrder%>&quot;>Date</a>

.. and so on....
 
Thanks fellas.
[tt]&quot;A Successful man is one who can build a firm foundation with the bricks that other throw at him&quot;[/tt]
:cool:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top