Professional Web Applications Themes

Creating a comma delimited list - ASP Database

Hi all, I have a table full of widgets. Each widget has its own detail page. I want a list page for the widgets where each widget is listed seperated by a comma. So my results will look like: Widget1, Widget2, Widget3, Widget 4 etc. I am having a huge problem getting this do output the way I want it to and am looking for a simple english explaination on how do get it to do what I want. My main problem is that I can get the commas after each one by looping through the records, but I cannot ...

  1. #1

    Default Creating a comma delimited list

    Hi all,

    I have a table full of widgets. Each widget has its own detail page. I
    want a list page for the widgets where each widget is listed seperated
    by a comma. So my results will look like:


    Widget1, Widget2, Widget3, Widget 4 etc.


    I am having a huge problem getting this do output the way I want it to
    and am looking for a simple english explaination on how do get it to
    do what I want. My main problem is that I can get the commas after
    each one by looping through the records, but I cannot strip off the
    comma at the end. Because the EOF is not the last record, but AFTER
    the last record.

    So my code basically looks like this.

    <%

    Dim rs, strSQL, strDelim
    strDelim = ", "

    strSQL = "SELECT WidgetID, Widget FROM tblWidget;"
    rs.Open strSQL, con

    Do While Not rs.EOF

    %>


    <A HREF="WidgetDetail.asp?WidID=<%=rs("WidgetID")%>"> <%=rsWidget("Widget")%></A><%=strDelim%>

    rs.MoveNext
    Loop

    rs.Close
    Set rs = Nothing

    %>


    Can someone show me an easy way to modify this code to display the
    results I want?

    Thanks in advance.
    Ryann Guest

  2. #2

    Default Re: Creating a comma delimited list

    Ryann wrote: 


    Instead of the slow, inefficient recordset loop, I suggest using GetRows and
    arrays:
    <%
    dim rs, strSQL, arData, arLinks(), sLinks, i
    const strDelim=","
    const adCmdText = 1

    strSQL = "SELECT WidgetID, Widget FROM tblWidget"
    set rs=con.execute(strsql,,adCmdText)
    if not rs.eof then arData = rs.GetRows
    rs.Close: Set rs=nothing
    con.close: Set con = nothing

    if isArray(arData) then
    redim arLinks(Ubound(arData,2))
    for i = 0 to Ubound(arData,2))
    arLinks(i)="<A HREF=""WidgetDetail.asp?WidID=" & _
    arData(0,i) & """>" & arData(1,i) & "</A>"
    next
    sLinks = Join(arLinks,strDelim)
    else
    'handle situation where query returned no records
    end if
    %>
    <HTML>
    ....
    <%=sLinks%>
    ....
    </HTML>

    HTH,
    Bob Barrows
    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"


    Bob Guest

  3. #3

    Default Re: Creating a comma delimited list

    I figured it out myself, here is how I did it..

    <%

    Dim rs, strSQL
    strSQL ="Select WidgetID, Widget From Widgets"
    rs.Open strSQL, con, 1, 1

    Delim = ", "
    TotRec = rs.RecordCount
    CurrRec = 0

    Do While Not rs.EOF
    CurrRec = CurrRec + 1
    %>

    <% If CurrRec = TotRec Then %>
    <A HREF="widget.asp?WidID=<%=rs("WidgetID")%>"><%=rs( "Widget")%></A>
    <% Else %>
    <A HREF="widget.asp?WidID=<%=rs("WidgetID")%>"><%=rs( "Widget")%></A><%=Delim%>
    <% End If %>

    <%
    rs.MoveNext
    Loop

    rs.Close
    Set rs = Nothing
    %>

    Thanks to anyone who took the time to read.

    com (Ryann) wrote in message news:<google.com>... 
    Ryann Guest

  4. #4

    Default Re: Creating a comma delimited list

    I really think you should try out Bob's code. While yours may work, it is
    far from optimal.

    --
    http://www.aspfaq.com/
    (Reverse address to reply.)




    "Ryann" <com> wrote in message
    news:google.com... 
    HREF="widget.asp?WidID=<%=rs("WidgetID")%>"><%=rs( "Widget")%></A><%=Delim%> 
    news:<google.com>... [/ref]
    HREF="WidgetDetail.asp?WidID=<%=rs("WidgetID")%>"> <%=rsWidget("Widget")%></A [/ref]


    Aaron Guest

Similar Threads

  1. Noob Ques Comma delimited list
    By Winston2 in forum Coldfusion - Getting Started
    Replies: 3
    Last Post: June 22nd, 07:00 PM
  2. comma delimited list problem
    By Shaun Perry in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: February 16th, 12:09 PM
  3. Splitting Comma delimited list
    By Colin Johnstone in forum PERL Beginners
    Replies: 10
    Last Post: December 30th, 04:01 AM
  4. Parsing Comma Delimited Strings
    By Word of Mouth Productions in forum Macromedia Director Lingo
    Replies: 5
    Last Post: September 17th, 08:38 PM
  5. generate CSV or comma delimited
    By shank in forum ASP
    Replies: 4
    Last Post: July 9th, 04:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139