Creating a comma delimited list

Creating a comma delimited list

am 03.10.2004 03:53:41 von fianna_sidhe

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

%>


<%=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.

Re: Creating a comma delimited list

am 03.10.2004 14:40:27 von reb01501

Ryann wrote:
> 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
>
> %>
>
>
> > HREF="WidgetDetail.asp?WidID=<%=rs("WidgetID")%>"><%=rsWidget("Widget")%><%=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.


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)="" & arData(1,i) & ""
next
sLinks = Join(arLinks,strDelim)
else
'handle situation where query returned no records
end if
%>

....
<%=sLinks%>
....


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"

Re: Creating a comma delimited list

am 03.10.2004 18:03:21 von fianna_sidhe

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 %>

<% Else %>
<%=Delim%>
<% End If %>

<%
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
%>

Thanks to anyone who took the time to read.

fianna_sidhe@hotmail.com (Ryann) wrote in message news:...
> 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
>
> %>
>
>
> <%=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.

Re: Creating a comma delimited list

am 03.10.2004 18:03:21 von fianna_sidhe

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 %>

<% Else %>
<%=Delim%>
<% End If %>

<%
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
%>

Thanks to anyone who took the time to read.

fianna_sidhe@hotmail.com (Ryann) wrote in message news:...
> 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
>
> %>
>
>
> <%=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.

Re: Creating a comma delimited list

am 04.10.2004 16:05:47 von ten.xoc

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" wrote in message
news:dab9ce46.0410030803.515f54c9@posting.google.com...
> 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 %>
>
> <% Else %>
> HREF="widget.asp?WidID=<%=rs("WidgetID")%>"><%=rs("Widget")%><%=Delim%>
> <% End If %>
>
> <%
> rs.MoveNext
> Loop
>
> rs.Close
> Set rs = Nothing
> %>
>
> Thanks to anyone who took the time to read.
>
> fianna_sidhe@hotmail.com (Ryann) wrote in message
news:...
> > 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
> >
> > %>
> >
> >
> > HREF="WidgetDetail.asp?WidID=<%=rs("WidgetID")%>"><%=rsWidget("Widget")%> ><%=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.

Re: Creating a comma delimited list

am 18.01.2005 08:49:53 von Bullschmidt

<<
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.
>>

Here is a code snippet that has worked for me.

' Put recs into array for speed.
arrayRS = objRS.GetRows

' Close rs.
objRS.Close
Set objRS = Nothing

' Init.
strEmails = ""

' Loop thru rows (i.e. recs).
For I = 0 To UBound(arrayRS, 2)
' Set var using each fld's col num.
Email = jpsvbNullToBlank(arrayRS(0, I))
strEmails = strEmails & ", " & Email
Next

' Remove initial comma and space.
strEmails = Right(strEmails, Len(strEmails) - 2)

Best regards,
J. Paul Schmidt, Freelance ASP Web Developer
http://www.Bullschmidt.com
ASP Design Tips, ASP Web Database Demo, Free ASP Bar Chart Tool...


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!