Solved

Sorting order ASC or DESC???

Posted on 2004-08-26
4
815 Views
Last Modified: 2008-03-17
I've got this working fine at the moment.

The only problem is it shows the results from the page oldest first and I want it to be newest first.

Heres the code I have written:

------------------------------------------------code--------------------------------------------------

<!--#include virtual="/dsn.asp" -->
<%

Set RS = Server.CreateObject("ADODB.Recordset")
RS.ActiveConnection = Conn
RS.CursorType = 3
RS.LockType = 3


pg = TRIM( Request( "pg" ) )
IF pg = "" THEN pg = 1

size = Request.QueryString("results")
IF size = "" THEN size = "6"

RS.PageSize = size

cate = Request.QueryString("cat")

sortBy = request("sort")
IF sortBy = "" THEN sortBy = "date"
request.form

sqlString = "SELECT * FROM guestbook ORDER BY " & sortBy
RS.Open sqlString

if not RS.EOF Then
RS.AbsolutePage = pg

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Guestbook</title>
<link rel="stylesheet" href="/styles/main.css" type="text/css">
<SCRIPT language="JavaScript">
      function submitform()
      {
        document.resort.submit();
      }
</SCRIPT>
</head>
<body bgcolor="#ffffff">
<!--#include virtual="/htdocs/header.shtml" -->
<table cellpadding="0" cellspacing="0" border="0" width="100%">
     <tr>
          <td rowspan="2" valign="top" class="shadow_v_l"><img src="/images/header/frame_l.jpg" width="15" height="9" border="0" alt=""></td>
          <td width="130"><img src="/images/header/shad_bg.jpg" width="130" height="9" border="0" alt=""></td>
          <td rowspan="2" valign="top" class="shadow_v_m"><img src="/images/header/frame_m.jpg" width="11" height="9" border="0" alt=""></td>
          <td class="shadow_h"><img src="/images/header/shad_bg.jpg" width="1" height="9" border="0" alt=""></td>
          <td rowspan="2" valign="top" class="shadow_v_r"><img src="/images/header/frame_r.jpg" width="14" height="9" border="0" alt=""></td>
     </tr>
     <tr>
          <td valign="top" id="leftnav">
<!--#include virtual="/htdocs/guestbook_leftnav.shtml" -->
          </td>
          <td valign="top" id="content">
               <div id="title">
                    <div class="left"><img src="/images/common/title_l.jpg" width="19" height="25" border="0" alt=""></div>
                    <div class="right"><img src="/images/common/title_r.jpg" width="19" height="25" border="0" alt=""></div>
                    <div class="box" align="center">Guestbook</div>
               </div>
               <div id="navbar">
                    <div class="left"><img src="/images/common/navbar_l.jpg" width="5" height="37" border="0" alt=""></div>
                    <div class="right"><img src="/images/common/navbar_r.jpg" width="5" height="37" border="0" alt=""></div>
                    <div class="box" align="center">
                                    <table cellpadding="0" cellspacing="0" border="0" width="90%" height="37">
                                          <tr>
                                                <td width="10%" nowrap>Sort by:</td>
                                                <td width="10%">
                                                <form action="guestbook.asp" method="post" name="resort">
                                                      <select name="sort">
                                                            <option value="date"<% if sortBy="date" then%> selected<%end if%>>Date</option>
                                                            <option value="name"<% if sortBy="name" then%> selected<%end if%>>Name</option>
                                                            <option value="country"<% if sortBy="country" then%> selected<%end if%>>Country</option>
                                                      </select>
                                                </form>
                                                </td>
                                                <td width="10%" align="center"><a href="javascript: submitform()"><img src="/images/common/go.gif" width="15" height="37" border="0" alt=""></a></td>
                                                <td align="right" width="65%"id="subheader">
                                                <%
                                                IF RS.PageCount > 1 THEN
                                                %>
                                                Go to page:
                                                <%
                                                  FOR i = 1 to RS.PageCount
                                                  IF i <> cINT( pg ) THEN
                                                %>

                                                <a href="/htdocs/guestbook.asp?pg=<%=i%>&sort=<%=sortBy%>">
                                                <%=i%></a>&nbsp;
                                                <% ELSE %>
                                                [<%=i%>]&nbsp;
                                                <% END IF %>
                                                <%
                                                  NEXT
                                                %>
                                                <%
                                                END IF
                                                %>
                                                </td>
                                          </tr>
                                    </table>
                              </div>
               </div>
               <div class="dotted_b"><img src="/images/common/blank.gif" width="1" height="1" border="0" alt=""></div>
<%
WHILE NOT RS.EOF AND rowCount < RS.PageSize
rowCount = rowCount + 1

%>
                        
                        <div id="subtitle">
                              <div class="left"><img src="/images/common/subtitle_l.jpg" width="5" height="19" border="0" alt=""></div>
                              <div class="right"><img src="/images/common/subtitle_r.jpg" width="5" height="19" border="0" alt=""></div>
                              <div class="box" align="left">
                                    <table cellpadding="0" cellspacing="0" border="0" width="95%" align="center">
                                          <tr>
                                                <td align="left" class="port_left"><%=RS ("name")%></td>
                                                <td align="right" class="port_left">Date: <%=RS ("date")%> Country: <%=RS ("country")%></td>
                                          </tr>
                                    </table>
                              </div>
                        </div>
                        <div class="port_text"><%=RS ("message")%></div>
                        <div class="dotted"><img src="/images/common/blank.gif" width="1" height="1" border="0" alt=""></div>
<%
RS.MoveNext
WEND
%>

          </td>
     </tr>
</table>

<!--#include virtual="/htdocs/footer.shtml" -->
</body>
</html>
<%
End if
%>
<!--#include virtual="/dsn2.asp" -->

------------------------------------------------code--------------------------------------------------

I'm using a query string value in the SQL statement so that if a person wants to reorder the results on the page they can:

sqlString = "SELECT * FROM guestbook ORDER BY " & sortBy

I've tried this to resort the results:

sqlString = "SELECT * FROM guestbook ORDER BY '" & sortBy & "' DESC "

This just srews up the order completely.

How can I change the order from oldest to newest first? One other thing is that the date I pull out of the database is in the wrong format. Is there away of changing the format of the date to the way we have it in the UK? at the moment its mm/dd/yyyy but it should be dd/mm/yyyy

Thanks in advance

Chris


0
Comment
Question by:chrissp26
  • 2
4 Comments
 
LVL 22

Expert Comment

by:neeraj523
ID: 11901456
Hello

Can you tell me the valye of this

Response.Write sqlString

add this line before opening the recordset..

also let me know the datatype of ur sort field.. which database you are using ?/

0
 
LVL 11

Accepted Solution

by:
mouatts earned 50 total points
ID: 11901630
sqlString = "SELECT * FROM guestbook ORDER BY '" & sortBy & "' DESC "

should read

sqlString = "SELECT * FROM guestbook ORDER BY " & sortBy & " DESC "

ie no single quotes around the field that you want to sort by. In addition you shouldn't have a field called date as this is a reserved word. You may be able to get away with calling it this within Access but it will or cause unpredictable problems when accessed via ADO/ODBC etc.

HTH
Steve
0
 
LVL 2

Author Comment

by:chrissp26
ID: 11902166
Yeah i'm using access as my database. Your line worked thanks for that. Should change all my date fields? I doubt i'm ever going to upsize the database to anything other than access but if you think its worth doing to prevent bad habits I'll do it.

neeraj523

by doing the response.write I get the following but mouatts solution worked great.

SELECT * FROM guestbook ORDER BY 'date' DESC

Thanks both of you for your help.

Chris

0
 
LVL 11

Expert Comment

by:mouatts
ID: 11905138
Yes you do need to change field names that use reserved words like date. It also work avoiding field names with spaces in as well, although access allows this and you can overcome problems when using ODBC/ADO etc why inccur the problems in the first place.

Steve
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now