Solved

Sorting order ASC or DESC???

Posted on 2004-08-26
4
825 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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