Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2013, 04:54 PM   PM User | #1
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
Unhappy appending to string variable while looping through records

This is my code. I am trying to add html for an acnhor link, to a string variable, each time a record is looped through in a recordset. If its the first record the html is slightly different.

My code as written only adds the first anchor, as if its ignoring the else in my if else statement based on the current recordset index.

- the code not working as expected is highlighted in red.

Code:
function view_NSMC_News(arg1)
dim mn,mnheader,mnfooter,mn_imgsrc,mn_title,mn_url,mn_content,objConn,objRs,sql,overlayPH,SlideNavHeader,SlideNavSquaresArray()
mnheader = "<div id=""slideshow"">"& vbCrLF
overlayPH = "<div id=""slideroverlay"">&nbsp;</div>"& vbCrLF
SlideNavHeader = "<div id=""slidenav"">"& vbCrLF
Response.Write mnheader

err.clear
On Error Resume Next

set objConn=server.CreateObject("ADODB.Connection")
objConn.ConnectionTimeout=5
objConn.Open testdsn
		Set objRs = Server.CreateObject("ADODB.Recordset")
 		objRs.Open "SELECT TOP "&arg1&" * FROM dbo.[12_NEWS] ORDER BY NEWID()", objConn, 3, 1
if Err.Number <> 0 then
	Response.write("<div class=""article img_left""><img src=""/images/nsmcconnect/blank.png"" width=""150"" height=""150"" alt=""No Image"" /><h2>Error Occured</h2><p>"&err.description&"</p></div>")
else
if objRs.BOF then 
		response.write("<div class=""article img_left""><img src=""/images/nsmcconnect/blank.png"" width=""150"" height=""150"" alt=""No Image"" /><h2>No Articles Found</h2><p>No current articles are posted at this time.</p></div>")
else
Do While Not objRs.EOF	
	   mn_id = objRs("ID_News")		
       mn_imgsrc = "/images/news/mainnews/"+objRs("Image1")
	   mn_title = objRs("Title")
	   mn_content = objRs("Article")
	   mn_url="/"

if objRS.Index = 0 then
 SlideNavSquares = "<a href=""/"" class=""active"">&nbsp;&nbsp;&nbsp;</a>"& vbCrLF
 else
 SlideNavSquares = SlideNavSquares & "<a href="/">&nbsp;&nbsp;&nbsp;</a>"& vbCrLF
 end if
Response.write("<a href="""&mn_url&""" class=""active"" alt="""&mn_title&"""><img src="""&mn_imgsrc&""" />"& vbCrLF)
Response.write("<div>"&mn_content&"<span class=""readmore"">>&nbsp;Read More</span></div></a>"& vbCrLF)			                               
objRs.MoveNext
Loop
end if                     
end if
objRs.close 
objConn.Close
Set objConn=Nothing 
Response.Write overlayPH
Response.Write SlideNavHeader 
Response.Write SlideNavSquares
Response.write("          </div>" & vbCrLF & "    </div>")'closing #slidenav
On Error GoTo 0
end function
if I change the number on objRS.Index = 0 , the results are the same as if it's not really checking it or something.

If I change to this

Code:
if objRS.Index = 0 then
 SlideNavSquares = SlideNavSquares & "<a href=""/"" class=""active"">&nbsp;&nbsp;&nbsp;</a>"& vbCrLF
 else
 SlideNavSquares = SlideNavSquares & "<a href="/">&nbsp;&nbsp;&nbsp;</a>"& vbCrLF
 end if
I get 2 html anchors, but the second anchor is incorrect, as if objRS.Index is matching 0 on every loop.. man im confused!
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

Last edited by DanInMa; 01-30-2013 at 06:40 PM..
DanInMa is offline   Reply With Quote
Old 01-30-2013, 06:40 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
nevermind i fixed it.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 01-30-2013, 09:07 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
LOL! Teach you to over-use ON ERROR RESUME NEXT, won't it?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-30-2013, 09:27 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I have to ask: Why are you opening the recordset with a static cursor??? You then only move forward through the recordset so you are wasting the performance expense of the cursor.

And why do you use
Code:
else
if objRs.BOF then
instead of
Code:
Elseif objRs.BOF then
(Not a big deal, but expresses what you are doing better.)

And by the by: You write out mnheader and overlayPH and more even when you have an error or you find no records (so there isn't really any slideshow). Is that intentional?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-30-2013, 09:37 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I can't help it. I'm a pedant.

Here's how I might have coded that:
Code:
Function view_NSMC_News(arg1)
    dim mn,mn_imgsrc,mn_title,mn_content,objConn,objRs,sql,SlideNavSquares,snsclass

    ' err.clear ' totally unneeded...On Error automatically does Clear as well

    On Error Resume Next
        set objConn=server.CreateObject("ADODB.Connection")
        objConn.ConnectionTimeout=5
        objConn.Open testdsn
	
	sql = "SELECT TOP "&arg1&" * FROM dbo.[12_NEWS] ORDER BY NEWID()"
	Set objRs = objConn.Execute( sql )
        if Err.Number <> 0 then
%>
	    <div class="article img_left">
                <img src="/images/nsmcconnect/blank.png" width="150" height="150" alt="No Image" />
                <h2>Error Occured</h2>
                <p><%=err.description%></p>
           </div>
<%
           Exit Function
       End If
    On Error GoTo 0 ' we *want* to see errors past here
   
    if objRs.EOF then 
%>
        <div class="article img_left">
            <img src="/images/nsmcconnect/blank.png" width="150" height="150" alt="No Image" />
            <h2>No Articles Found</h2>
            <p>No current articles are posted at this time.</p>
        </div>
<%
        Exit Function
    End If
    ' okay...if we get here there really is a slideshow to show:
%>
    <div id="slideshow">
<%
    snsClass = " class=""active"" "
    SlideNavSquares = ""

    Do While Not objRs.EOF	
        mn_id = objRs("ID_News")		
        mn_imgsrc = "/images/news/mainnews/" & objRs("Image1")
	mn_title = objRs("Title")
	mn_content = objRs("Article")
        SlideNavSquares = SlideNavSquares _
                        & "<a href=""/"" " & snsclass & ">&nbsp;&nbsp;&nbsp;</a>"& vbCrLF
%>
        <a href="/" class="active" alt="<%=mn_title%>"><img src="<%=mn_imgsrc%>" />
            <div>
                <%=mn_content%>
                <span class="readmore">&lt;&nbsp;Read More</span>
            </div>
        </a>
<%
        snsclass = "" ' so only first <a> is active
        objRs.MoveNext
    Loop
    objRs.close ' not really needed...ending the function ends these variables' lives
    objConn.Close
%>
        <div id="slideroverlay">&nbsp;</div>
        <div id="slidenav">
            <%=SlideNavSquares%>
        </div><!--slidenav-->
    </div><!--slideshow-->
<%
End Function
%>
That would also execute a bit faster. Not much. A few milliseconds, perhaps.

Getting rid of Response.Write is the biggest performance boost.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 01-30-2013 at 09:40 PM..
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
DanInMa (01-31-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:36 PM.


Advertisement
Log in to turn off these ads.