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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

better way to do loads of ifs

Status
Not open for further replies.

coolicus

Programmer
May 15, 2007
50
GB
I have a page that I need to use asp to check if a database field has data, if it has display one image if not display another.

There are 10 times I would need to do this, is there a better way of doing it than how I am doing it? I guess I am using too many resources and slowing the page down too.

Code:
<%
if rs("field1") <> "" then
response.write "<img src='image1.gif'>"
else
response.write "<img src='image2.gif'>"
end if

if rs("field2") <> "" then
response.write "<img src='image1.gif'>"
else
response.write "<img src='image2.gif'>"
end if

if rs("field3") <> "" then
response.write "<img src='image1.gif'>"
else
response.write "<img src='image2.gif'>"
end if

etc etc etc at the necessary points in the table.
 
[tt]'dim a, e 'dim somewhere if needed
a=array("field1","field2","field3") 'etc etc
for each e in a
if rs(e)<>"" then
response.write "<img src='image1.gif'>"
else
response.write "<img src='image2.gif'>"
end if
next[/tt]
 
Another option, iterate the recordset while calling a function. This only cleans up the code

e.g.

Do While NOT rs.EOF
DBCheckVal(rs value)
rs.MoveNext
Loop

Function DBCheckVal(parm)
if rs("field3") <> "" then
DBCheckVal= "<img src='image1.gif'>"
else
DBCheckVal= "<img src='image2.gif'>"
end if
End Function

[sub]____________ signature below ______________
You may get help by means of code here. Just recall what happens to a rat when he eats the cheese off a rat trap while you're Ctrl+C/Ctrl+V'ing that code as your own[/sub]
 
if rs("field3") <> "" then
to
if parm <> "" then

jus tin case you didn't notice my copy/paste typo

[sub]____________ signature below ______________
You may get help by means of code here. Just recall what happens to a rat when he eats the cheese off a rat trap while you're Ctrl+C/Ctrl+V'ing that code as your own[/sub]
 
Using a loop or Function() for 10 elements will not significantly change the computational load on the server. The same question must still be answered for each field. (Q: Is this field empty?)

So if you are looking to increase execution speed this will not be the answer.

That said, a loop or a function is a cleaner way to do this and probably easier to maintain over time so it is worth doing even without any speed boost.
 
Why do you think that block of code is a source of poor performance? Have you run some tests checking the time to build the page with one check versus 10 checks?

I dont think 10 IF statements or a 100 IF statements are going to affect the page in a noticeable way. Maybe the problem lies elsewhere.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top