Google Answers Logo
View Question
 
Q: Nested loop/if...then TABLE display problem (ASP) ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Nested loop/if...then TABLE display problem (ASP)
Category: Computers > Programming
Asked by: krickles-ga
List Price: $100.00
Posted: 19 Jun 2003 17:41 PDT
Expires: 19 Jul 2003 17:41 PDT
Question ID: 219472
I am returning a recordset with the following query:

*************** [CODE] ********************
SELECT sdl.supplier_division_id,
       s.supplier_id,
       s.supplier_name,
       sdl.status_id,
       sl.status_desc
FROM   supplier AS s
INNER JOIN supplier_division_lu AS sdl ON sdl.supplier_id =
s.supplier_id
INNER JOIN division_lu AS d ON d.division_id = sdl.division_id
INNER JOIN status_lu AS sl ON sl.status_id = sdl.status_id
ORDER BY s.supplier_name
*************** [/CODE] ******************

The query returns the correct information.  However, I am having a
difficult time displaying the information.  Currently, I am using the
following ASP/HTML:

*************** [CODE] ********************
Set rsGetList = objRec
rsGetList.MoveFirst

...some html code with <table> and <th> 

Do While NOT rsGetList.EOF
Response.Write ("<TD>")
Response.Write rsGetList("supplier_name")
     IF varSupplier_ID = rsGetList("supplier_id") THEN
          Response.Write ("<TD>")
          Response.Write ("<INPUT type = checkvox id=checkbox1 name=")
          Response.Write ("'" & rsGetList("division_name") & "'")
          Response.Write (rsGetList("status_desc"))
          Response.Write (">")
          Response.Write rsGetList("division_name")
          Response.Write ("<BR>")
          Response.Write ("</TD>")
     END IF
Response.Write ("<TD>")

...html buttons

Response.Write ("</TR">)
rsGetList.MoveNext
Loop
*************** [/CODE] *******************

The table diplays the following:

SUPPLIER A | DIVISION 1 | BUTTONS
-------------------------------------------
SUPPLIER A | DIVISION 2 | BUTTONS
-------------------------------------------
SUPPLIER A | DIVISION 3 | BUTTONS
-------------------------------------------
SUPPLIER B | DIVISION 1 | BUTTONS
-------------------------------------------
SUPPLIER B | DIVISION 2 | BUTTONS

I would like the table to display as follows:

           | DIVISION 1 | 
SUPPLIER A | DIVISION 2 | BUTTONS
           | DIVISION 3 | 
-------------------------------------------
SUPPLIER B | DIVISION 1 | BUTTONS
           | DIVISION 2 | 

The example directly above should have only two rows as opposed to
five rows in the other table display example.  Please advise.  Kind
regards,  Krickles

Clarification of Question by krickles-ga on 19 Jun 2003 17:46 PDT
By the way, I will be traveling all day tomorrow and will not be able
to clarify anything until Friday night (at the earliest).  Krickles
Answer  
Subject: Re: Nested loop/if...then TABLE display problem (ASP)
Answered By: thinkout-ga on 19 Jun 2003 19:13 PDT
Rated:5 out of 5 stars
 
Greetings Krickles!

I am posting a code snippet below.  Make a backup of your original asp
before you do anything else.  Try these changes in a COPY of your page
so that you can always roll back to your version if you run into any
problems.

Okay, with that said, in the copy of your page, delete all of the text
from the characters "... some html code with" all the way through the
word "loop" on the bottom and replace it with the code below.  There
are comments included that should answer most of your questions, but
please let me know if you need clarification on any fine points.

This should create the table structure you are looking for.  Without
your complete code to look at, there may need to be a few tweaks. 
Again, let me know if I can clarify anything.  Happy coding!

Sincerely,

Thinkout-ga

******************** [CODE] ******************** 
Dim lSupplierID, lLastSupplierID 

'Start with mis-matched values (Fires the first 'if' event) 
'Assumes your valid results set does NOT have an id of -1 
lLastSupplierID = -1 
lSupplierID = CDbl(rsGetList("supplier_id")) 

Do While NOT rsGetList.EOF 

If lLastSupplierID <> lSupplierID Then 

'We're assuming the <tr> has already been opened above... 
'Write the supplier cell 
Response.Write ("<TD>") 
Response.Write rsGetList("supplier_name") 
Response.Write("</td>" & vbCrLf) 

            'Open the cell for the division names 
      Response.Write ("<TD>") 
End If 

      'Write out the division names and check boxes 
'check box, name and id should match 
Response.Write ("<INPUT type=checkbox id=") 
Response.Write ("'" & rsGetList("division_name") & "' name=") 
Response.Write ("'" & rsGetList("division_name") & "' ") 

'Does status_desc return 'Checked'? 
Response.Write (rsGetList("status_desc")) 
Response.Write (">") 

'Write the name and the break for the next 
Response.Write rsGetList("division_name") 
Response.Write ("<BR>") 

'Iterate the control variable 
lLastSupplierID = lSupplierID 

'Get the next record 
rsGetList.MoveNext 

If NOT rsGetList.EOF Then 

'Get the next row's supplier ID 
lSupplierID = CDbl(rsGetList("supplier_id")) 

'Check if we have changed Suppliers 
If lLastSupplierID <> lSupplierID Then 

'Close the cell 
Response.Write ("</TD>" & vbCrLf) 

'Open the buttons cell 
Response.Write ("<TD>") 
'...html buttons 

'Close the buttons cell 
Response.Write("</td>" & vbCrLf) 

'Close the row 
Response.Write ("</TR>" & vbCrLf) 

'This is not eof, so open a new row 
Response.Write ("<TR>" & vbCrLf) 
End If 
Else 

'Close the cell 
Response.Write ("</TD>" & vbCrLf) 

'Open the buttons cell 
Response.Write ("<TD>") 
'...html buttons 

'Close the buttons cell 
Response.Write("</td>" & vbCrLf) 

'Close the row 
Response.Write ("</TR>" & vbCrLf) 
End If 
Loop 
******************** [CODE] ********************
krickles-ga rated this answer:5 out of 5 stars and gave an additional tip of: $35.00
Beautiful.  I worked on this for days, asked several people, and
submitted on another forum.  I had someone print your answer out for
me before I left LA yesterday.  While in route home I attempted to
type in the code, but still couldn’t get it to work as requested.  I
looked for typos for hours and even re-entered the code.  I shouldn’t
have been doing so at the end of a long week, because, invariably, I
was too tired to focus.  Anyway, I just copied & pasted your code into
my app and it worked exactly as requested.  Thanks so much for the
help.  Regards,  Krickles

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy