...

View Full Version : response.write asp code that executes



Gary Williams
04-09-2006, 08:29 AM
Hi All,

I am displaying the contents of 3 fields from an access database using the following code:

<tr><td height="20"></td>
<tr><td bgcolor="#eeeeee">Page 1</td><td><TEXTAREA WRAP=PHYSICAL ID="page1" NAME="page1" ROWS=20 COLS=120 ><%=(ors.Fields.Item("page1").Value)%></TEXTAREA></td></tr>
<tr><td height="20"></td>
<tr><td bgcolor="#c0c0c0">Page 2</td><td><TEXTAREA WRAP=PHYSICAL ID="page2" NAME="page2" ROWS=20 COLS=120 ><%=(ors.Fields.Item("page2").Value)%></TEXTAREA></td></tr>
<tr><td height="20"></td>
<tr><td bgcolor="#eeeeee">Page 3</td><td><TEXTAREA WRAP=PHYSICAL ID="page3" NAME="page3" ROWS=20 COLS=120 ><%=(ors.Fields.Item("page3").Value)%></TEXTAREA></td></tr>
<tr><td height="20"></td>

This works perfectly.

I'm now trying to be clever and replace the above code with a loop. This almost works:

<%
Dim i
For i=1 to 3

response.write "<tr><td height=""20""></td>"
response.write "<tr><td bgcolor=""#eeeeee"">Page "&i&"</td><td><TEXTAREA WRAP=PHYSICAL ID=""page"&i&""" NAME=""page"&i&""" ROWS=""20"" COLS=""120"" >"
response.write "<%=(ors.Fields.Item(""page"&i&""").Value)% >"
response.write "</TEXTAREA></td></tr>"

next
%>

The problem is the end of this 3rd row:

response.write "<%=(ors.Fields.Item(""page"&i&""").Value)% >"

If I close up the space between the " % " and the " > " the script doesn't work.

How can I get this line of code [response.write "<%=(ors.Fields.Item(""page"&i&""").Value)% >"] to produce this [><%=(ors.Fields.Item("page2").Value)%>]?

Regards

Gary

chud_wallice
04-09-2006, 09:42 AM
hi gary
you could try something like this.
i didn't test it and it depends what fields you have in your db.
If you have two fields say one for the name and then one for the textarea content then you could loop through it any number of ways.
I always think its better not to hard code a loop value with the recordset object (unless your always only pulling 3 items)



while not ors.EOF
Response.Write ("<tr><td bgcolor='#eeeeee'>"& _
ors("pageName") & "</td>" &vbcrlf & _
"<td><TEXTAREA WRAP=PHYSICAL ID='" &_
ors("pageName") & "' NAME='" & ors("pageName") & _
"' ROWS='20' COLS='120'>" & ors("page") & _
"</TEXTAREA></td></tr>")
ors.MoveNext
wend



hth

Gary Williams
04-09-2006, 10:00 AM
Hi Chud,

Thanks for the help. It produced and interesting result in that it displayed both the rendered html and the code in a textarea.

I'll experiment with this.

Thanks again.

Regards

Gary

Gary Williams
04-09-2006, 10:23 AM
Hi Chud,

OK, got this far:

<%
Dim i
For i=1 to 3
Response.Write "<tr><td bgcolor=""#eeeeee"">Page " & i & "</td><td><TEXTAREA WRAP=PHYSICAL ID=""Page" & i & """ NAME=""Page" & i & """ ROWS=""20"" COLS=""120"">" & ors("'"page"'" "'"&i&"'") & "</TEXTAREA></td></tr>"
next
%>

The code written by this is correct excetp for the part in bold. I'm trying to loop through the fields by incrementing the field name - page1, page2, etc.

I need it to write:

"& ors("page1") & "
"& ors("page2") & "

and so on.

Regards

Gary

miranda
04-09-2006, 03:12 PM
OK, got this far:

<%
Dim i
For i=1 to 3
Response.Write "<tr><td bgcolor=""#eeeeee"">Page " & i & "</td><td><TEXTAREA WRAP=PHYSICAL ID=""Page" & i & """ NAME=""Page" & i & """ ROWS=""20"" COLS=""120"">" & ors("'"page"'" "'"&i&"'") & "</TEXTAREA></td></tr>"
next
%>

The code written by this is correct excetp for the part in bold. I'm trying to loop through the fields by incrementing the field name - page1, page2, etc.


from your code example, you have database fields with the same name except for a numeric identifier at the end like page1, page2, page3

using two quotes like so "" says that you want to display a single quote on the page. Generally speaking you only need to use quote apostrophe quote when you are inside a sql statement working with a datatype that isn't numeric. When you want to return the value of a column in a recordset then you just use a single quote around the name of the column. Since you want to loop through the different like named columns you would use the single quote around the name plus the & and the variable that holds the value so you end up with



<%
Dim i
For i=1 to 3
Response.Write "<tr><td bgcolor=""#eeeeee"">Page " & i & "</td><td><TEXTAREA WRAP=PHYSICAL ID=""Page" & i & """ NAME=""Page" & i & """ ROWS=""20"" COLS=""120"">" & ors("page"&i) & "</TEXTAREA></td></tr>"
next
%>




you could also do it like this


Dim i, sPage
For i=1 to 3
sPage = "page" & i
Response.Write "<tr><td bgcolor=""#eeeeee"">Page " & i & "</td><td><TEXTAREA WRAP=PHYSICAL ID=""" sPage & """ NAME=""" & sPage & """ ROWS=""20"" COLS=""120"">" & ors(sPage) & "</TEXTAREA></td></tr>"
next



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum