Solved

Sorting order ASC or DESC???

Posted on 2004-08-26
4
813 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

14 Experts available now in Live!

Get 1:1 Help Now