SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Zealot ok_hornet's Avatar
    Join Date
    May 2009
    Location
    Oklahoma
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question need help with fancy if statement

    Hello all,

    I have the following if statements as part of a larger loop:
    Code ASP:
    for each x in rs.Fields
    	if x.name = "HSDisplay" then
    		HSDisplay = x.value
    		if x.value = "true" then
    			bgClass = "hs"
    		end if
    	end if
    	if x.name = "MSDisplay" then
    		MSDisplay = x.value
    			if x.value = "true" then
    			bgClass = "ms"
    		end if
    	end if
    	if x.name = "CherDisplay" then
    		CherDisplay = x.value
    		if x.value = "true" then
    			bgClass = "cher"
    		end if
    	end if
    	if x.name = "GWDisplay" then
    		GWDisplay = x.value
    		if x.value = "true" then
    			bgClass = "grwd"
    		end if
    	end if
    	if x.name = "SeqDisplay" then
    		SeqDisplay = x.value
    		if x.value = "true" then
    			bgClass = "seq"
    		end if
    	end if
    	if x.name = "DistrictDisplay" then
    		DistrictDisplay = x.value
    	end if
     
    	' final if statement
    next

    For the "final if statement" I need something to look at the other if statements before it and decide if >2 of the variables from the if statements = "true" then set bgClass = "district".

    I thought about something like below, but haven't got it to work yet.
    Code ASP:
    for each x in rs.Fields
     
    	i = 0
     
    	if x.name = "HSDisplay" then
    		HSDisplay = x.value
    		if x.value = "true" then
    			i = i + 1
    			bgClass = "hs"
    		end if
    	end if
    	if x.name = "MSDisplay" then
    		MSDisplay = x.value
    			if x.value = "true" then
    			i = i + 1
    			bgClass = "ms"
    		end if
    	end if
    	if x.name = "CherDisplay" then
    		CherDisplay = x.value
    		if x.value = "true" then
    			i = i + 1
    			bgClass = "cher"
    		end if
    	end if
    	if x.name = "GWDisplay" then
    		GWDisplay = x.value
    		if x.value = "true" then
    			i = i + 1
    			bgClass = "grwd"
    		end if
    	end if
    	if x.name = "SeqDisplay" then
    		SeqDisplay = x.value
    		if x.value = "true" then
    			i = i + 1
    			bgClass = "seq"
    		end if
    	end if
     
    	if x.name = "DistrictDisplay" then
    		DistrictDisplay = x.value
    		if x.value = "true" then
    			i = i + 1
    		end if
    	end if
     
    	if i >= 3 then
    		bgClass = "district"
    	end if
     
    next

    Loops, arrays, etc. Whatever I need to rewrite for it to work is fine with me. Any help would be greatly appreciated!
    Follow me: @josh_max

  2. #2
    SitePoint Guru
    Join Date
    Jun 2007
    Posts
    690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what do you mean it "doesn't work"

    looks like it should work - what do you get when you check the output of the variable i during the loops ?

    debugging is the key to solving this problem.

  3. #3
    SitePoint Zealot ok_hornet's Avatar
    Join Date
    May 2009
    Location
    Oklahoma
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it. The placement of the i=0 was key.

    It needed to be placed between the "do until" and the "for each".

    Placing the i=0 before the do until caused i to continually get added to itself while placing the i=0 after the for each, inside the loop, kept resetting it to 0 as soon as it was added to (weird).

    Here's my full loop for those interested:

    Code ASP:
    <%
     do until rs.EOF
    	i = 0
    	for each x in rs.Fields
     
    		if x.name = "EventName" then
    			title = x.value
    		end if
    		if x.name = "EventStartDate" then
    			startDate = x.value
    		end if
    		if x.name = "EventEndDate" then
    			endDate = x.value
    		end if
    		if x.name = "EventTime" then
    			eventTime = x.value
    		end if
    		if x.name = "EventDescription" then
    			details = x.value
    		end if
    		if x.name = "EventLocation" then
    			location = x.value
    		end if
     
    		if x.name = "HSDisplay" then
    			HSDisplay = x.value
    			if x.value = "true" then
    				i = i + 1
    				bgClass = "hs"
    			end if
    		end if
    		if x.name = "MSDisplay" then
    			MSDisplay = x.value
    			if x.value = "true" then
    				i = i + 1
    				bgClass = "ms"
    			end if
    		end if
    		if x.name = "CherDisplay" then
    			CherDisplay = x.value
    			if x.value = "true" then
    				i = i + 1
    				bgClass = "cher"
    			end if
    		end if
    		if x.name = "GWDisplay" then
    			GWDisplay = x.value
    			if x.value = "true" then
    				i = i + 1
    				bgClass = "grwd"
    			end if
    		end if
    		if x.name = "SeqDisplay" then
    			SeqDisplay = x.value
    			if x.value = "true" then
    				i = i + 1
    				bgClass = "seq"
    			end if
    		end if
    		if x.name = "DistrictDisplay" then
    			DistrictDisplay = x.value
    			if x.value = "true" then
    				i = i + 1
    			end if
    		end if
    		if i >= 3 then
    			bgClass = "district"
    		end if
    	next
    %>
    <li>
    	<div class="<%=bgClass%>">
    		<h4><%=title%><!-- <%=i%> for debugging --></h4>
    		<h5><%
    response.write("<span>" & startDate & "</span>")
    if endDate <> "" then 
    	response.write(" to <span>" & endDate & "</span>") 
    end if
    if eventTime <> "" then 
    	response.write(" begins @ <span class='time'>" & left(eventTime, 4))
    	if right(eventTime, 2) = "AM" then
    		response.write("am </span>")
    	else
    		response.write("pm </span")
    	end if
    end if
    if location <> "" then 
    	response.write("@ <span>" & location & "</span>")
    end if
    		%></h5>
    		<p><%=details%></p>
    	</div>
    </li>
     
    <%
     rs.MoveNext
     Loop
    %>

    Thanks for the response, webber.
    Follow me: @josh_max

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah! Such an error wouldn't be obvious without seeing the scope in which the For Each was being used.
    Ian Anderson
    www.siteguru.co.uk


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •