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?
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?