problem with nested loop (I think)

M

middletree

A few days ago, I posted a problem, and the answer given has led me to this
point. I think I have the idea right, but the syntax isn't quite there.

Using classic ASP, I have a page which allows users to enter some info into
a table called Personal.

Also on the same form is some checkboxes, which are populated by a mostly
static table we'll call Gift.

Because it's a many-to-many relationship (that is, a user can check more
than one checkbox), I have created a table we'll refer to as table
PersonalGift.

It's a union table for resolving the many-to-many between tables Personal
and Gift. It consists of only two fields: the ID (Primary Key) of each,
called PersonaID and GiftID.

This works fine, but now I'm asked to create an edit page. A user needs to
be able to click a name, which will take them to a page which displays the
information which was entered in the original form. For most of the info,
which was stored in the Personal table, this is no problem. But I need to be
able to list all 25 checkboxes (dynamically built from the Gift table), and
if there is a match for that person (using the Personal table's PK), I need
to have that checkbox to be checked, and if not, it needs to be unchecked.

Just to be clear, the objective here is to populate, on page load, certain
checkboxes, depending on what'salreay in the database.

It seems like I could do this by doing a SELECT from table PersonalGift:
SELECT GiftID
FROM PersonalGIft
WHERE PersonalID = 2 (2 being an example, pulled in from the querystring)

If it turns out that there are rows in table PersonalGift like this:
PersonalID GiftID

2 2
2 4
2 8


this would give me a result set of (for example) 2,4,8

So I just have to build my checkboxes to display the word "checked" if the
ID of that checkbox is somewhere in that resultset.

As everyone here probably knows, in HTML, a checkbox which is prefilled with
a check in the checkbox on page load looks like this:

<input type=checkbox value=whatever checked>

in contrast, a checkbox with the below code will not be checked on page
load:

<input type=checkbox value=whatever>


Anyway, that's my goal here. Display the checkboxes, and display certain
ones as checked with the page loads.

So here's how I am doing it. Assuming I use the SQL code mentioned earlier,
I will have a resultset that contains all the IDs for all 25 boxes. We'll
call that rsAllBoxes. The resultset containing the checked ones (2,4,8)
should ideally be put into an array:

arrChecked = rsChecked.GetRows()

Now we iterate the array as many times as we like, which we want to do each
time we write a checkbox:

Do Until rsAllBoxes.EOF
Response.Write "<input type=""checkbox"" id=""" & rsAllBoxes("ID") & """"
For i = 0 To Ubound(arrChecked,2)
If Cint(arrChecked(0,i) = Cint(rsAllBoxes("ID") Then Response.Write "
checked=""checked"""
Next
Response.Write ">" & rsAllBoxes("TextValue") & "<br />"
rsAllBoxes.MoveNext
Loop




Needless to say, it's displaying all sorts of VB Script errors.

Any ideas?
 
E

Evertjan.

middletree wrote on 23 jan 2009 in
microsoft.public.inetserver.asp.general:
Do Until rsAllBoxes.EOF
Response.Write "<input type=""checkbox"" id=""" & rsAllBoxes("ID") &
"""" For i = 0 To Ubound(arrChecked,2)
If Cint(arrChecked(0,i) = Cint(rsAllBoxes("ID") Then Response.Write "

You are missing some closing ))) here

Cint(arrChecked(0,i)) = Cint(rsAllBoxes("ID"))


HTML ERROR: a numeric is iss not allowed,
all id-s should start with a letter.

Seems to me you probably ment:
Cint(arrChecked(2,i))
???
checked=""checked"""

Writing a space & the word 'checked' suffices,
Are you sure you never write it more that once?
Next
Response.Write ">" & rsAllBoxes("TextValue") & "<br />"

/> ???
Are you writing xml?
rsAllBoxes.MoveNext
Loop

You should look at the rendered source as provided by your
browser and most could be revealed, as I sullose there are some missing
spaces.

For better visibility and not having to count the """"" use:

<form ...>
<%
Do Until rsAllBoxes.EOF
%>
<input type='checkbox' id='<%=rsAllBoxes("ID")%>'
<%
''' warning, probable numeric id html error '''

temp = ""
For i = 0 To Ubound(arrChecked,2)
If Cint(arrChecked(0,i)) = Cint(rsAllBoxes("ID")) Then
temp = " checked"
End If
Next
%>
<% = temp%>> <% = rsAllBoxes("TextValue") %><br>
<%
rsAllBoxes.MoveNext
Loop

%>
</form>
 
A

Adrienne Boswell

Writing a space & the word 'checked' suffices,
Are you sure you never write it more that once?

Depending on the flavor of the HTML, XHTML cannot have empty attributes, so
it must be checked="checked" for XHTML.
 
E

Evertjan.

Adrienne Boswell wrote on 23 jan 2009 in
microsoft.public.inetserver.asp.general:
Depending on the flavor of the HTML, XHTML cannot have empty
attributes, so it must be checked="checked" for XHTML.

Sorry, my influenza precludes me from tasting flavors today.
 
A

Adrienne Boswell

Adrienne Boswell wrote on 23 jan 2009 in
microsoft.public.inetserver.asp.general:


Sorry, my influenza precludes me from tasting flavors today.

Hope you feel better soon. Try this, it works every time for me.
Take a clove of fresh garlic and cut it up into pieces about the same
size as a vitamin, then take those garlic pieces with a glass of water.
Have a cup of tea with some honey, then bundle yourself up and sleep for
a few hours. Fever should break pretty easily, and you'll feel better.
 
M

middletree

HTML ERROR: a numeric is iss not allowed,
all id-s should start with a letter.

This ID is numeric because that's the datatype in the database. I wasn't
aware that this was incorrect HTML. I will have to go back and look. I might
be using the "name" attribute instead of the ID.
Writing a space & the word 'checked' suffices,

I think I did this because it's been a practice of mine for years to place
atrtibutes inside quotes. Doesn't the W3C validator require this for HTML 4
and XHTML?
/> ???
Are you writing xml?

I think this page, which I am writing in Visual Studio, is declared as
XHTML. I'd be just fine with HTML 4, of course, so maybe I'll go check that
out. Thanks for pointing me to that.

As for the code snippet you provided, I was able to use that, with some
adjustments, and solve this problem. Thanks for your help, and I hope you
get to feeling better soon.
 
A

Adrienne Boswell

This ID is numeric because that's the datatype in the database. I
wasn't aware that this was incorrect HTML. I will have to go back and
look. I might be using the "name" attribute instead of the ID.

You could do something like:
<%for i = 1 to 10%>
<div id="a_<%=i%>">Something</div>
I think I did this because it's been a practice of mine for years to
place atrtibutes inside quotes. Doesn't the W3C validator require this
for HTML 4 and XHTML?

HTML is checked, XHTML is checked="checked"
I think this page, which I am writing in Visual Studio, is declared as
XHTML. I'd be just fine with HTML 4, of course, so maybe I'll go check
that out. Thanks for pointing me to that.

Just be sure that which ever one you use you use the correct syntax for
empty elements, eg: <img> (HTML) <img /> XHTML. Don't mix, and make
sure there is a DocType, Strict is best.
 
M

middletree

Just be sure that which ever one you use you use the correct syntax
for empty elements, eg: <img> (HTML) <img /> XHTML. Don't mix, and
make sure there is a DocType, Strict is best.


I am glad you both pointed this out. As it turns out, I have:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

which means I need to go back and make sure that there aren't any stray
xhtml strings around, like the ones you describe above. My excuse is that
it's a really old page and I am editing it, but really, there's no excuse
for this sloppiness.

Interestingly,. I'm doing most of my testing in IE7, which is still
displaying it correctly.
 
A

Adrienne Boswell

Gazing into my crystal ball I observed "middletree"
<[email protected]> writing in


We're getting off topic here (ms.public.iis.asp.general), so I have set
follow-ups to alt.html.
I am glad you both pointed this out. As it turns out, I have:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

For new pages, you really want strict, for pages that are in transition,
transitional will do.
which means I need to go back and make sure that there aren't any
stray xhtml strings around, like the ones you describe above. My
excuse is that it's a really old page and I am editing it, but really,
there's no excuse for this sloppiness.

Yes, better to clean them up and use strict.
Interestingly,. I'm doing most of my testing in IE7, which is still
displaying it correctly.

I'm sure you know IE7 is buggy. It's better to test in a more compliant
browser, like Opera or Firefox. Usually, I do my main testing in Opera,
then if something doesn't display correctly, I debug in Firefox, and
finally, I _might_ have a look in IE.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top