Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rows and colums

Status
Not open for further replies.

mazdaman

Programmer
Oct 17, 2003
55
GB
How do I display a result set in, say, 3 colums and as many rows as it takes to show the complete set ? There are not many items in the database - 12.

Cheers
 
Something like this:

<table>
<tr>
<td> column1</td>
<td> column2</td>
<td> column3</td>
</tr>
<%DO UNTIL rs.EOF %>
<tr>
<td><%=rs(column2value)%></td>
<td><%=rs(column2value)%></td>
<td><%=rs(column2value)%></td>
<%
rs.MoveNext
Loop
%>

-VJ
 
I think i maen that the results will populate across the 3 colums then loop through and build the rows as needed ... will this code do this ? I.E. show me all the names in the names field and build the table thuse .. populate from left to right and then down

fred, george, allan,
andrew, terry, colin,
paul, peter, andrew,
zane, graham, tony,

Cheers
 
Use a counter to keep track, then start a new row each time the counter mod 3 = 0

I'll provuide an example although you really should check the FAQs and search past posts on this, it has been asked a couple hundred times...
Code:
'pretend we have a recordset called objRS
CONST COL_COUNT = 3
If Not objRS.EOF Then objRS.MoveFirst
Dim col_ctr
col_ctr = 0   'not really necessary
Response.Write "<table><tr><th colspan=""" & COL_COUNT & """>Results</th></tr>"
Do Until objRS.EOF
   If col_ctr mod COL_COUNT = 0 Then Response.Write "<tr>"

   Response.Write "<td>" & objRS("filedname") & "</td>"

   If col_ctr mod COL_COUNT = COL_COUNT - 1 Then Response.Write "</tr>"

   col_cnt = col_cnt + 1
Loop
Response.Write "</table>"

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
for random datasets, i prefer to use the ADO components to do the work :

Code:
Set con = Server.Crea....
Con.Open Blah

Set RS = Con.Execute(blah)
' Checks for Records, errors no data or makes table
If not RS.Eof Then
%>
<table>
<%
' Makes the Column Headers with the field names
  Response.Write "<tr>"
  For each field in Rs.Fields
    Response.Write "<td><b>" & Field.Name & "</b></td>"
  Next
  Response.Write "</tr>"
' Cycle Records and Sub Cycle Fields
  Do While Not Rs.EOF
    Response.Write "<tr>"
    For each field in Rs.Fields
      Response.Write "<td>" & Server.HTMLEncode(RS(Field.Name)) & "</td>"
    Next
    Response.Write "</tr>"
    RS.MoveNext
  Loop
%>
</table>
<%
Else
  response.write "No data"
End If
Set RS = nothing
Con.Close
Set Con = nothing
 
Cheers DreXor but being only a technical user, where would the code go ? Here the page

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/igd.asp" -->
<%
Dim rows
Dim rows_numRows

Set rows = Server.CreateObject("ADODB.Recordset")
rows.ActiveConnection = MM_igd_STRING
rows.Source = "SELECT * FROM plants"
rows.CursorType = 0
rows.CursorLocation = 2
rows.LockType = 1
rows.Open()

rows_numRows = 0
%>
<html>
<head>
<title>Test Rows</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

</body>
</html>
<%
rows.Close()
Set rows = Nothing
%>
 
with your code starting at the commented line in mine (checks records) paste the remainder of the sample code into yours just after your : rows.open()

now i used RS in mine, you can use RS by replacing rows for rs or use rows and replace the rs's in mine to rows.
 
Cheer DreXor - been away for a while - tried your code with the following error - Object doesn't support this property or method: 'igdnumber'
Here is the complete code I run (the field name that I am trying to disply is called - igdnumber.

Codeeeeeeee
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/igd.asp" -->
<%
Dim rows
Dim rows_numRows

Set rows = Server.CreateObject("ADODB.Recordset")
rows.ActiveConnection = MM_igd_STRING
rows.Source = "SELECT * FROM plants"
rows.CursorType = 0
rows.CursorLocation = 2
rows.LockType = 1
rows.Open()
If not rows.Eof Then
%>
<table>
<%
' Makes the Column Headers with the field names
Response.Write "<tr>"
For each field in rows.Fields
Response.Write "<td><b>" & igdnumber & "</b></td>"
Next
Response.Write "</tr>"
' Cycle Records and Sub Cycle Fields
Do While Not rows.EOF
Response.Write "<tr>"
For each field in rows.Fields
Response.Write "<td>" & Server.HTMLEncode(rows.igdnumber) & "</td>"
Next
Response.Write "</tr>"
rows.MoveNext
Loop
%>
</table>
<%
Else
response.write "No data"
End If
Set rows = nothing
Con.Close
Set Con = nothing

rows_numRows = 0
%>
<html>
<head>
<title>Test Rows</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
test rows
</body>
</html>
<%
rows.Close()
Set rows = Nothing
%>
 
Cheers Tarwin - here is the code I used but the page just times out when running with no errors generated
Codeeee...
<% CONST COL_COUNT = 3
If Not abc.EOF Then abc.MoveFirst
Dim col_ctr
col_ctr = 0 'not really necessary
Response.Write "<table><tr><th colspan=""" & COL_COUNT & """>Results</th></tr>"
Do Until abc.EOF
If col_ctr mod COL_COUNT = 0 Then Response.Write "<tr>"

Response.Write "<td>" & abc("igdnumber") & "</td>"

If col_ctr mod COL_COUNT = COL_COUNT - 1 Then Response.Write "</tr>"

col_cnt = col_cnt + 1
Loop
Response.Write "</table>"%>
 
what happened to the code i pasted and the code you tried is that you changed out too many items ..
leave the code as i supplied, but replace RS with your recordset object (ROWS)

[thumbsup2]DreX
aKa - Robert
 
Heh, and people say I don't make mistakes.

Ahem


The error in my original post was that I forgot to put ion the MoveNext...so while I am sure you have been enjoying the numerous breaks you have inadvertantly received because the page was eating the CPU for about 5 minutes every time you ran it, it's now time to get back to work. :)

Correction:
Code:
<% CONST COL_COUNT = 3
If Not abc.EOF Then abc.MoveFirst
Dim col_ctr
col_ctr = 0   'not really necessary
Response.Write "<table><tr><th colspan=""" & COL_COUNT & """>Results</th></tr>"
Do Until abc.EOF
   If col_ctr mod COL_COUNT = 0 Then Response.Write "<tr>"

   Response.Write "<td>" & abc("igdnumber") & "</td>"

   If col_ctr mod COL_COUNT = COL_COUNT - 1 Then Response.Write "</tr>"

   col_cnt = col_cnt + 1
   [highlight]abc.MoveNext[/highlight]
Loop
Response.Write "</table>"%>

Pages with loops inthem used to be guaranteed cigarette breaks because I always forgot the darn movenext's...guess I still forget them sometimes :p

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Hi Tarwin - made changes and get results back BUT displays just the one colum. Wanted it to basically show 3 results (from the same field)in a row then wrap down to the next row - end product would be 3 colums and as many rows as it takes to display the results of a single field (idgnumber). Would it be easy to add another field to this code ? i.e.

field1 field1 field1
field2 field2 field2

field1 field1 field1
field2 field2 field2

field1 field1 field1
field2 field2 field2

Bare with me im such a novice - and you guys are so much help !! Cheers all
 
Thanks DreXtor - Ill try my best. Does your code support 2 fields as suggested in reply to Tarwin.

Can you also suggest a book or something that will be a good grounding for getting to grips with ASP ? I live in the UK

Thanks for help
 
DreXtor had the following error ...Type mismatch: 'Server.HTMLEncode'

Here is the full code ...
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="Connections/igd.asp" -->
<%
Dim rows
Dim rows_numRows

Set rows = Server.CreateObject("ADODB.Recordset")
rows.ActiveConnection = MM_igd_STRING
rows.Source = "SELECT * FROM plants"
rows.CursorType = 0
rows.CursorLocation = 2
rows.LockType = 1
rows.Open()
If not rows.Eof Then
%>
<table>
<%
' Makes the Column Headers with the field names
Response.Write "<tr>"
For each field in rows.Fields
Response.Write "<td><b>" & Field.Name & "</b></td>"
Next
Response.Write "</tr>"
' Cycle Records and Sub Cycle Fields
Do While Not rows.EOF
Response.Write "<tr>"
For each field in rows.Fields
Response.Write "<td>" & Server.HTMLEncode(rows(Field.Name)) & "</td>"
Next
Response.Write "</tr>"
rows.MoveNext
Loop
%>
</table>
<%
Else
response.write "No data"
End If
Set rows = nothing
Con.Close
Set Con = nothing

rows_numRows = 0
%>
<html>
<head>
<title>Test Rows</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
hello
</body>
</html>
<%
rows.Close()
Set rows = Nothing
%>
 
wow, i must have been really tired when i wrote that thing, I just noticed it was only supposed to be an example, not working code. The variable name for the counter is wrong, so it wsn't incrmeneting correctly:
Code:
<% CONST COL_COUNT = 3
If Not abc.EOF Then abc.MoveFirst
Dim col_ctr
col_ctr = 0   'not really necessary
Response.Write "<table><tr><th colspan=""" & COL_COUNT & """>Results</th></tr>"
Do Until abc.EOF
   If col_ctr mod COL_COUNT = 0 Then Response.Write "<tr>"

   Response.Write "<td>" & abc("igdnumber") & "</td>"

   If col_ctr mod COL_COUNT = COL_COUNT - 1 Then Response.Write "</tr>"

   'used to be: col_cnt = col_cnt + 1
   col_ctr = col_ctr + 1
   abc.MoveNext
Loop
Response.Write "</table>"%>

Anyways, easy fix.

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
No, two fields shouldn't be a big deal. You could basically create a string for the second row while your outputting the first row. Every time you end a row, write the string out (to be the next row) then clear the string:
Code:
<% CONST COL_COUNT = 3
If Not abc.EOF Then abc.MoveFirst
Dim col_ctr, sec_row
col_ctr = 0   'not really necessary
sec_row = ""

Response.Write "<table><tr><th colspan=""" & COL_COUNT & """>Results</th></tr>"
Do Until abc.EOF
   If col_ctr mod COL_COUNT = 0 Then 
      Response.Write "<tr>"
      rec_row = "<tr>"
   End If

   Response.Write "<td>" & abc("igdnumber") & "</td>"
   sec_row = sec_row & "<td>" & abc("whatever") & "</td>"

   If col_ctr mod COL_COUNT = COL_COUNT - 1 Then 
      Response.Write "</tr>"
      Response.Write sec_row & "</tr>"
      Response.Write "<tr><td>&nbsp;</td></tr>" 'blank row
   End If

   'used to be: col_cnt = col_cnt + 1
   col_ctr = col_ctr + 1
   abc.MoveNext
Loop
Response.Write "</table>"%>

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 
Cheers Tarwin it seem to work fine but the second field seems to multipl its self check out
Here is the code i have used ...there are 2 fields ID=database key, size=large or small

<% CONST COL_COUNT = 3
If Not abc.EOF Then abc.MoveFirst
Dim col_ctr, sec_row
col_ctr = 0 'not really necessary
sec_row = ""

Response.Write "<table width=350><tr><th colspan=""" & COL_COUNT & """>Results</th></tr>"
Do Until abc.EOF
If col_ctr mod COL_COUNT = 0 Then
Response.Write "<tr>"
rec_row = "<tr>"
End If

Response.Write "<td>" & abc("ID") & "</td>"
sec_row = sec_row & "<td>" & abc("size") & "</td>"

If col_ctr mod COL_COUNT = COL_COUNT - 1 Then
Response.Write "</tr>"
Response.Write sec_row & "</tr>"
Response.Write "<tr><td>&nbsp;</td></tr>" 'blank row
End If

'used to be: col_cnt = col_cnt + 1
col_ctr = col_ctr + 1
abc.MoveNext
Loop
Response.Write "</table>"%>
 
it's self appending, you just need to reset the value of the variable on each loop through although the way this is assembled it seems to be writing the record out each passthrough, then reaching count of 3 or zero *boggles* then writing itself again, there's abundant </tr> but very few <tr> in the source from the link supplied.

the whole +1 -1 and use of Mod is confusing me, but perhaps these notes might be able to help Tarwn patch it up:
Code:
Do Until abc.EOF
   If col_ctr mod COL_COUNT = 0 Then 
      Response.Write "<tr>" ' [red]<< one opened[/red]
      rec_row = "<tr>" ' [red]<< [b]not used?[/b][/red]
   End If

   Response.Write "<td>" & abc("ID") & "</td>" ' [red]<<< Writes every pass through RS loop[/red]
   sec_row = sec_row & "<td>" & abc("size") & "</td>" '[red]<<< stores every pass through[/red]

   If col_ctr mod COL_COUNT = COL_COUNT - 1 Then 
      Response.Write "</tr>" '[red] << one closed[/red]
      Response.Write sec_row & "</tr>" ' [red]<< [b]two[/b] closed[/red]
      Response.Write "<tr><td>&nbsp;</td></tr>" 'blank row
   End If

   'used to be: col_cnt = col_cnt + 1
   col_ctr = col_ctr + 1
   ' [red] sec_row  never set = "", self stacking, [b]possibly rec_row from above?[/b] [/red]
   abc.MoveNext
Loop
Response.Write "</table>"

[thumbsup2]DreX
aKa - Robert
 
Stop making fun of my horrible keyboard :)

Yet another typo in that, which is why I always use variable declaration and Option Explicit in my code:
Code:
Do Until abc.EOF
   If col_ctr mod COL_COUNT = 0 Then
      Response.Write "<tr>" ' << one opened
      [highlight]sec_row = "<tr>" ' rec_row was supposed to be sec_row :P[/highlight]
   End If

   Response.Write "<td>" & abc("ID") & "</td>" ' <<< Writes every pass through RS loop
   sec_row = sec_row & "<td>" & abc("size") & "</td>" '<<< stores every pass through

   If col_ctr mod COL_COUNT = COL_COUNT - 1 Then
      Response.Write "</tr>" ' << one closed
      Response.Write sec_row & "</tr>" ' << two closed
      Response.Write "<tr><td>&nbsp;</td></tr>" 'blank row
   End If

   'used to be: col_cnt = col_cnt + 1
   col_ctr = col_ctr + 1
   ' sec_row  never set = "", self stacking, possibly rec_row from above?
   abc.MoveNext
Loop
Response.Write "</table>"

By setting sec_row = "<tr>" in the beginng we clar out the previous value, it wasn't supposed to stack multiple rows, it's just this dang cheap $9.99 keyboard. Maybe I should start posting from my laptop more instead (though something feels wrong about posting on the ASP forum from a Linux machine :p)

-T

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
The never-completed website:
 

It works - cheers . it was the keybored, changed rec for sec and it all feel together ! Thats great code - just trying to get my head around the code. Im on a course for ASP soon and just trying to get some pratice ...

So what if i wanted to take the detail of an idividual record, say size, and open the full detail on a page called detail.asp ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top