Link to home
Start Free TrialLog in
Avatar of Wayne Barron
Wayne BarronFlag for United States of America

asked on

Generate Rows and Columns in Order (Working Code supplied, need to work with RS)

Hello All;

This code works great, and is needed badly to work with a RecordSet.
I have tried and tried and cannot get it to work properly.

Below is the working code example. Then the 2nd one is my attempt of making it work with
A RS.

If someone has a better way of doing this, I am all for it.
If not, if someone can assist with this code that would be great as well.
'Working code
<% ' Generates 20 Records per row [x] 5 columns
for i = 1 to 100
response.write  i & "<br />"
if i mod 20 = 0 then
response.write "</td><td>"
end if
next
%>
 
 
' This is with a RS, works but not properly
 
<% ' Generates 100 records per row [x] 5 columns (Has the columns right, but the Rows not)
for i = 1 to 100
st = objRs("MyTitle")
response.write  st & "<br />"
if i mod 20 = 0 then
response.write "</td><td>"
end if
objRS.MoveNext
next
wend
%>
 
'Have even tried with this
<%
for i = 1 to objRs.PageSize   ' getting the amout for the page itself, still will not work
st = objRs("MyTitle")
response.write  st & "<br />"
if i mod 20 = 0 then
response.write "</td><td>"
end if
objRS.MoveNext
next
wend
%>

Open in new window

Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Try this: (This assumes you have a record set returning an exact number of items to to display 100 items per table row.)

Dim index
index = 1
While not objRS.EOF
  Response.Write objRs("MyTitle")  & "<br />"
  if index = 20 then
    response.Write "</td><td>"
    index = 1
  else
    index = index + 1
  end if
  objRS.MoveNext
Wend
Avatar of Wayne Barron

ASKER

I am not wanting to have 100 items per row.
I want to have 20 items per row.

I stated in my post that it was incorrectly displaying 100 items per row.

Will test your code now
Trying to get it to work in my project in a pain.
So I tested it in a test page, and it works BUT
I need it to show only
20 per row
5 Columns

Right now it is displaying
20 per row (Correct)
And not sure how many Columns,, A lot of them.
I need 5 columns
I am sorry.
Your code works great.

I just need to get it to work in my demo project which has a Paging System
And a limit for how many items are to be shown.

I am having one of those days, I am so very sorry for doubting your code.

Carrzkiss
I was right.
I need it to have columns.
Is there a way to pre-define the amount of columns shown?

I just got it to work in my project which is a paging project, and it list the whole db
In my DB I have over 1000 entries.
In my real-world project I have 3 entries at the moment but within the coming months this number will go over 100+ entries.

Any idea's on the columns.
Needs to be a set column to   [5]
Basically
20 per row   [x]   5 Columns.

Thanks
Carrzkiss
Your original post had code that looped through 100 items and created a new table cell after every 20th item. That code, however, did not include anything for create the begin or end table and row tags (<tr></tr>) so I left it out of the code I provided. The code below expands on the code I provided earlier by adding in the table tags and table row tags. If you need to control the number of table rows displayed (TR), you can add an additional While loop around the begin/end TR tags.

You can control the number of items displayed in each cell by modifying the value assigned to MAX_ITEMS_PER_CELL, and you can control the number of cells in each table row by modifying the value assigned to MAX_CELLS_PER_ROW.

Depending on how your data is retrieved and the type of database it is coming from, you may want to investigate the possibility of performing the paging on the database side, as it will limit the amount of data you are transferring from it to your web server.
Const MAX_CELLS_PER_ROW = 5
Const MAX_ITEMS_PER_CELL = 20
Dim itemIndex, cellIndex
 
Response.Write "<table>"
 
While Not objRS.EOF
  cellIndex = 0
  itemIndex = 0
  Response.Write "<tr>"  ' Creates the beginning table row tag
 
  While cellIndex < MAX_CELLS_PER_ROW
    Response.Write "<td>"
    While itemIndex < MAX_ITEMS_PER_CELL
      Response.Write objRs("MyTitle") & "<br />"
      objRS.MoveNext
      If objRS.EOF Then Exit While
      itemIndex = itemIndex + 1
    Wend
    Response.Write "</td>"
    If objRS.EOF Then Exit While
    cellIndex = cellIndex + 1
  Wend
 
  While cellIndex < MAX_CELLS_PER_ROW
    Response.Write "<td>&nbsp;</td>"
  Wend
  Response.Write "</tr>"  ' Creates the ending table row tag
Wend
Response.Write "</table>"

Open in new window

The idea of doing it on the database side would be something that I would love to be able to do in the near future.
Right now on the site, there is going to be less than 100 items total until the site gets more people involvedm then it will need to get it's paging on the database side.

So.
If you have some information on doing this on the database side that would be great.

And also.
Thanks for the lesson, and the knowledge.
Always great to gain more knowledge.

(Going to test the code now)
Carrzkiss
And now it is "Error Time"

OK.
Ran the code and it generates the following error

Microsoft VBScript compilation error '800a040f'

Invalid 'exit' statement

/EE/Q_24675792/Columns/test.asp, line 44

If objRS.EOF Then Exit While
-----------------------^
I fixed the code I provided and tested locally:
  Const MAX_CELLS_PER_ROW = 5
  Const MAX_ITEMS_PER_CELL = 20
  Dim itemIndex, cellIndex
 
  If objRs.EOF Then
    Response.Write "No Data Found"
  Else
    Response.Write "<table border='2'>"
    
    Do While Not objRS.EOF
      cellIndex = 0
      Response.Write "<tr>"  ' Creates the beginning table row tag
      Do While cellIndex < MAX_CELLS_PER_ROW
        Response.Write "<td>"
        itemIndex = 0
        Do While itemIndex < MAX_ITEMS_PER_CELL
          Response.Write objRs("MyTitle") & "<br />"
          objRS.MoveNext
          If objRS.EOF Then Exit Do
          itemIndex = itemIndex + 1
        Loop
        Response.Write "</td>"
        cellIndex = cellIndex + 1
        If objRS.EOF Then Exit Do
      Loop
 
      While cellIndex < MAX_CELLS_PER_ROW
        Response.Write "<td>&nbsp;</td>"
        cellIndex = cellIndex + 1
      Wend
      Response.Write "</tr>"  ' Creates the ending table row tag
    Loop
    Response.Write "</table>"
  End If

Open in new window

That is nicer, but still produces multiple column rows.
Please have a look here
http://ee.cffcs.com/Q_24676312/test.asp  (Simple page)
http://ee.cffcs.com/WILL NOT LOAD/WILL NOT LOAD  cr.asp (With a paging system involved)
(The paging Does not want to load, so best that you download and run from your system
It loads fine on mine, just not from the server and on through the Internet.)
Code
http://ee.cffcs.com/Q_24676312/Q_24676312.zip

I need it to have just 1 section
20 per row
5 Columns
------------
That is it, as it sits right now, the code here gives me the results which is GREAT!!
But it repeats the results 13 times
Is the test.asp page what you are getting or what you want? (The zip file link gives me a page not found error.)
Shaun
I am so sorry, you can access the zip file now. It was mis-named

The test.asp page has everything on that page, that is not what I want
I need what you supplied except I need it to only have on the page
Limit of

5 Columns [x] 20 per row.

As you can see in then cr.asp file that is in the zip file.
I have it in a paging system, so that once you hit the limit of the
5 Columns [x] 20 per row (Will be 100 records displayed)
And then you page over to the next set of names.

Carrzkiss
ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are AWESOME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Thank you so very much.
Thank you for showing that we all can continue to learn.
That no one knows it all, that someone always knows something that you may not..

Thank You
Carrzkiss
You are AWESOME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Thank you so very much.
Thank you for showing that we all can continue to learn.
That no one knows it all, that someone always knows something that you may not..

Thank You
Carrzkiss
For all that may need it, here it is

http://ee.cffcs.com/Q_24676312/cr3.asp
code
http://ee.cffcs.com/Q_24676312/Q_24676312.zip

Shaun did an awesome job on this one, as there seems to be a lack of people that
Know how to do this one.
Through searching the net and a lot of trial and error I finally posted the question to
See if someone can do what no ones else had accomplished.
But knowing that he had been done and could be done in ASP Classic.
I thank Shaun and tip my hat to him for his great workmanship and coding.

You have taught me and a lot of others how to do something new.

Thank You Shaun
Carrzkiss