The best method to list by columns from a db

P

PiGei

hi all,
I'm using the following code to read articles from a db and show them by
columns and not by rows.
I think the code could be better... so, if you have some advice...

thanks

db= access

<%
Dim num_col 'number of columns
Dim end_row 'checking enf of row
Dim counter

num_col = 2
end_row = "false"
counter = 1
%>

<table border="1" cellpadding="2" cellspacing="2" width="450">
<%
Do While Not rstSimple.EOF
%>
<%
If end_row = "false" Then
response.write "<tr>"
End If
%>
<td>
<%= rstSimple.Fields("id").Value %><br>
<%= rstSimple.Fields("titolo").Value %><br>
<%= rstSimple.Fields("data").Value %><br>
<%= "end_row= " & end_row %><br>
<%= "counter= " & counter %><br>
<%= "num_col= " & num_col %><br>
</td>
<%
rstSimple.MoveNext
%>
<%
If counter = num_col OR rstSimple.EOF Then
response.write "</tr>"
end_row = "false"
counter = 1
%>
<%
Else
%>
<% counter = counter+1 %>
<% end_row = "true" %>
<% End If %>
<%
Loop
%>
</table>

Thanks
 
D

Dave Anderson

PiGei said:
hi all,
I'm using the following code to read articles from a db and show them
by columns and not by rows.
I think the code could be better... so, if you have some advice...

You might want to consider using GetRows():
http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetrows.asp


This is why (thanks to ASP FAQ):

"By its very nature, the 2-dimensional array returned by the GetRows
method is non-linear. This has important implications for the ways
in which the result set data can be processed. The data in the result
set can be processed in a row-by-row, column-by-column or cell-by-cell
manner, or any combination there-of. Formatting can also take place at
the row, column and/or cell level."

http://aspfaq.com/show.asp?id=2467



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
P

PiGei

Thanks
PGei

Dave Anderson said:
You might want to consider using GetRows():
http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetrows.asp


This is why (thanks to ASP FAQ):

"By its very nature, the 2-dimensional array returned by the GetRows
method is non-linear. This has important implications for the ways
in which the result set data can be processed. The data in the result
set can be processed in a row-by-row, column-by-column or cell-by-cell
manner, or any combination there-of. Formatting can also take place at
the row, column and/or cell level."

http://aspfaq.com/show.asp?id=2467



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message.
Use of this email address implies consent to these terms. Please do not
contact me directly or ask me to contact you directly for assistance. If
your question is worth asking, it's worth posting.
 
B

Bob Barrows [MVP]

PiGei said:
hi all,
I'm using the following code to read articles from a db and show them
by columns and not by rows.
I think the code could be better... so, if you have some advice...

thanks

db= access

<%
Dim num_col 'number of columns
Dim end_row 'checking enf of row
Dim counter

num_col = 2
end_row = "false"
counter = 1
%>

<table border="1" cellpadding="2" cellspacing="2" width="450">
<%
Do While Not rstSimple.EOF
%>
<%
If end_row = "false" Then
response.write "<tr>"
End If
%>
<td>
<%= rstSimple.Fields("id").Value %><br>
<%= rstSimple.Fields("titolo").Value %><br>
<%= rstSimple.Fields("data").Value %><br>
<%= "end_row= " & end_row %><br>
<%= "counter= " & counter %><br>
<%= "num_col= " & num_col %><br>
</td>
<%
rstSimple.MoveNext
%>
<%
If counter = num_col OR rstSimple.EOF Then
response.write "</tr>"
end_row = "false"
counter = 1
%>
<%
Else
%>
<% counter = counter+1 %>
<% end_row = "true" %>
<% End If %>
<%
Loop
%>
</table>

Thanks

This should be better:
<%
Dim num_col 'number of columns
dim iRows, iRow
Dim iCol
dim arData, i, j
dim arHTML(), sHTML, sCell

num_col = 2
arData = rstSimple.GetRows

rstSimple.close:set rstSimple=nothing
'close and destroy your connection here as well

if isArray(arData) then
sHTML = "<TABLE border=""1"" cellpadding=""2""" & _
" cellspacing=""2"" width=""450"">"
iRows = ubound(arData,2) \ num_col
if ubound(arData,2) mod num_col > 0 then iRows = iRows+1
reDim arHTML(iRows)
for i = 0 to ubound(arData,2)
iCol= (i mod num_col) + 1
iRow=(i \ num_col) + 1
if iCol = 1 then
sCell = "<TR><TD>"
elseif sCol < num_col then
sCell=sCell & "<TD>"
end if
sCell=sCell & arData(0,i)
for j=1 to 2
sCell = sCell & "<BR>" & arData(j,i)
next
sCell = sCell & "<BR>Column= " & iCol
sCell = sCell & "<BR>Row= " & iRow & "</TD>"
if iCol = num_col then
arHTML(iRow - 1) = sCell & "</TR>"
elseif i = ubound(arData,2) then
do until iCol = num_col
sCell = sCell & "<TD> &nbsp;</TD>"
iCol=iCol+1
loop
arHTML(iRow - 1) = sCell & "</TR>"
end if
next
sHTML = sHTML & join(arHTML,vbCrLf) & "</TABLE>"
else
sHTML = "No data was returned"
end if
Response.Write sHTML
%>

HTH,
Bob Barrows
 
P

PiGei

Thanks Bob
PGei

Bob Barrows said:
This should be better:
<%
Dim num_col 'number of columns
dim iRows, iRow
Dim iCol
dim arData, i, j
dim arHTML(), sHTML, sCell

num_col = 2
arData = rstSimple.GetRows

rstSimple.close:set rstSimple=nothing
'close and destroy your connection here as well

if isArray(arData) then
sHTML = "<TABLE border=""1"" cellpadding=""2""" & _
" cellspacing=""2"" width=""450"">"
iRows = ubound(arData,2) \ num_col
if ubound(arData,2) mod num_col > 0 then iRows = iRows+1
reDim arHTML(iRows)
for i = 0 to ubound(arData,2)
iCol= (i mod num_col) + 1
iRow=(i \ num_col) + 1
if iCol = 1 then
sCell = "<TR><TD>"
elseif sCol < num_col then
sCell=sCell & "<TD>"
end if
sCell=sCell & arData(0,i)
for j=1 to 2
sCell = sCell & "<BR>" & arData(j,i)
next
sCell = sCell & "<BR>Column= " & iCol
sCell = sCell & "<BR>Row= " & iRow & "</TD>"
if iCol = num_col then
arHTML(iRow - 1) = sCell & "</TR>"
elseif i = ubound(arData,2) then
do until iCol = num_col
sCell = sCell & "<TD> &nbsp;</TD>"
iCol=iCol+1
loop
arHTML(iRow - 1) = sCell & "</TR>"
end if
next
sHTML = sHTML & join(arHTML,vbCrLf) & "</TABLE>"
else
sHTML = "No data was returned"
end if
Response.Write sHTML
%>

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top