Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with loop 1

Status
Not open for further replies.

Ciarrai

Technical User
Jun 27, 2003
17
US
Hi,
This is probably very simple but for some reason I can't get this loop to work properly. I am looping through a column in my database. If a field is found in the column a checked box is displayed along with the name of the field. If it is not found I want to display an unchecked box.
I've tried a single outer loop using if statments as well but it prints out too many checkboxes...

Here is the code:
<table width="490" border = "1">
<tr>
<td width="38">
<% Do while not rsCategories.EOF
if rsCategories("Category") = "Football" then %>
<INPUT TYPE="CHECKBOX" NAME="Football" value="Football" checked = "true"></td>
<% else %>
<input TYPE="CHECKBOX" NAME="Football" VALUE="Football"></td>
<%end if
rsCategories.moveNext()
loop %>
<td width="130">Football</td>
</tr>

<tr>
<td width="38">
<% rsCategories.moveFirst()
Do while not rsCategories.EOF
if rsCategories("Category") = "Hurling" then %>
<INPUT TYPE="CHECKBOX" NAME="Hurling" value = "Hurling" checked = "true"></td>
<% else %>
<INPUT TYPE="CHECKBOX" NAME="Hurling" value = "Hurling"></td>
<% end if
rsCategories.moveNext()
loop %>
<td width="130">Hurling</td>
</tr>

...same procedure continues down to the end of the table

I'd appreciate if anyone could show me the light!
Thanks
Ciarrai
 
the largest problem I see rate away is the cell tags

as it is now you open a cell in the table (TD) but only close it multiple times. so to fix that you need to open/close a cell on each condition of the data found

eg:
Code:
<tr>
<% 
Do while not rsCategories.EOF
  if rsCategories("Category") = "Football" then 
%>
  <td width="38"><INPUT TYPE="CHECKBOX" NAME="Football" value="Football" checked = "true"></td>
<% 
else 
%>
  <td width="38"><input TYPE="CHECKBOX" NAME="Football" VALUE="Football"></td>
<%
  end if

rsCategories.moveNext()
loop 
%>

___________________________________________________________________

The answer to your ??'s may be closer then you think.
Check out Tek-Tips knowledge bank by clicking the FAQ link at the top of the page
 
I don't know if this is the whole code, but if it is, you may want to consider rsCategories.MoveFirst ...

rsCategories.MoveFirst
Do While Not rsCategories.EOF
..... your stuff ......
rsCategories.MoveNext
Loop
 
Thank you for your replies.
I added in the extra <td> tags and the rsCategories.moveFirst. However, i still can't get the loop to work properly. If I have three options in the database I get 3 columns. If i Have 4 options in the database I get 4 columns. The checked checkboxes are appearing correctly but there is just too many columns. Any ideas??


<%
rsCategories.moveFirst()
Do while not rsCategories.EOF
if rsCategories("Category") = "Football" then
%>
<td width="38"><INPUT TYPE="CHECKBOX" NAME="Football" value="Football" checked = "true"></td>
<%
else
%>
<td width="38"><input TYPE="CHECKBOX" NAME="Football" VALUE="Football"></td>
<%
end if
rsCategories.moveNext()
loop
%>


Ciarrai
 
Now that I've thoroughly read your post . . .

If the column isn't found, how will the loop actually know what to do? I would suggest having a column in the database for "TurnedOn" set to an "int" . . . then, if "TurnedOn" = 1 then, check it . . . if "TurnedOn" = 0 then don't .....

=================
<%
strsql = "select * from myDatabase"
set rsCategories = objConnection.execute(strsql)
%>
<table>
<%
rsCategories.moveFirst
Do while not rsCategories.EOF or rsCategories.BOF
strcategory = rsCategories.Fields("Category")
strTurnedOn = rsCategories.Fields("TurnedOn")
if strTurnedOn = 1 then
strselected = "checked"
else
strselected = ""
end if

%>
<tr>
<td width="38">
<input type="checkbox" name="<%=rsCategories.fields("Category")%>" <%=strselected%>>
</td>
</tr>
<%
rsCategories.moveNext
loop
%>

===============

OR, if you want 2 per row, then just do something like:

===============

<%
strsql = "select * from myDatabase"
set rsCategories = objConnection.execute(strsql)
%>
<table>
<%
'//////// set a counter
intcount = 0
'//////// move to first record
rsCategories.moveFirst
Do while not rsCategories.EOF or rsCategories.BOF
'//////// gater the field's information
strcategory = rsCategories.Fields("Category")
strTurnedOn = rsCategories.Fields("TurnedOn")
'/////// if it's turned on, let's set the "selected" value
'/////// 1 = it is selected
'/////// 0 = it is not selected
if strTurnedOn = 1 then
strselected = "checked"
else
strselected = ""
end if
'/////// add one to the counter
intcount = incount + 1

'/////// if the counter is set to 1, we'll need to start a row
if intcount = 1 then
response.write "<tr>"
end if
'/////// write out the table cell
%>
<td width="38">
<input type="checkbox" name="<%=rsCategories.fields("Category")%>" <%=strselected%>>
</td>
<%
'/////// if we started a new row and put two cells into it
'/////// we need to close the row
if intcount = 2 then
response.write "</tr>"
'/////// and reset the counter to zero
intcount = 0
end if
'/////// do it all again
rsCategories.moveNext
loop
%>
</table>

 
Thanks for you help. That worked perfectly!
Ciarrai
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top