I'm using ColdFusion to pull UK postcodes into an array for display on a Google Map. This happens dynamically from a SQL database, so the numbers can range from 1 to 100+
the script works great, however, in IE (groan) it decides to display one point way off line, over in California somewhere.
I fixed this issue in a previous webapp, this was due to the ma between each array item still being present at the end. Works fine in Firefox, Safari etc, but not IE.
But, that one was using a set 10 records, so was easy to fix.
I just need a little if statement to wrap around my ma to hide it when it hits the last record. I can't seem to get it right. Any tips/suggestions?
here is the line of code in question:
var address = [<cfloop query="getApplicant"><cfif getApplicant.dbHomePostCode GT ""><cfoutput>'#getApplicant.dbHomePostCode#',</cfoutput></cfif> </cfloop>];
Hopefully someone can help with this rather simple request. I'm just having a bad day at the office!
I'm using ColdFusion to pull UK postcodes into an array for display on a Google Map. This happens dynamically from a SQL database, so the numbers can range from 1 to 100+
the script works great, however, in IE (groan) it decides to display one point way off line, over in California somewhere.
I fixed this issue in a previous webapp, this was due to the ma between each array item still being present at the end. Works fine in Firefox, Safari etc, but not IE.
But, that one was using a set 10 records, so was easy to fix.
I just need a little if statement to wrap around my ma to hide it when it hits the last record. I can't seem to get it right. Any tips/suggestions?
here is the line of code in question:
var address = [<cfloop query="getApplicant"><cfif getApplicant.dbHomePostCode GT ""><cfoutput>'#getApplicant.dbHomePostCode#',</cfoutput></cfif> </cfloop>];
Hopefully someone can help with this rather simple request. I'm just having a bad day at the office!
Share Improve this question asked Mar 31, 2010 at 10:38 Simon HumeSimon Hume 1,0123 gold badges12 silver badges28 bronze badges 2- @Simon: In fact, Internet Explorer is the only one of the pack that gets it right. The trailing space in an array declaration is a JavaScript syntax violation, and choking at a syntax error is not a "bug". JavaScript is not HTML, after all. The other browsers make JS debugging difficult because of their leniency. – Tomalak Commented Mar 31, 2010 at 12:01
- Yes, i realised this after posting my anti-IE hate campaign. I think 'bug' was probably the wrong word to use on this occasion – Simon Hume Commented Mar 31, 2010 at 12:26
5 Answers
Reset to default 5var address = [#ListQualify(ValueList(getApplicant.dbHomePostCode), "'")#]
I notice a <cfif getApplicant.dbHomePostCode GT "">
in your code.
With ListQualify()
the empty (NULL or empty string) post codes will not show up in the output, since ColdFusion list functions ignore empty list elements.
EDIT: The previous revision of this answer indicated that empty elements would show up in the result of ListQualify()
. This is incorrect, but the first two ments refer to this initial revision.
My approach is a little different, you have to do less setup nor do you have to iterate; it's all done the the cfloop tag.
var address = [
<cfloop from="1" to="#getApplicant.recordcount#" index="i">
<cfif getApplicant.dbHomePostCode GT "">
<cfoutput>
'#getApplicant.dbHomePostCode[i]#'
<cfif i lt getApplicant.recordcount>,</cfif>
</cfoutput>
</cfif>
</cfloop>
];
Here it is without the breaks, you should be able to paste this into your page and it'll work.
var address = [<cfloop from="1" to="#getApplicant.recordcount#" index="i"><cfif getApplicant.dbHomePostCode GT ""><cfoutput>'#getApplicant.dbHomePostCode[i]#'<cfif i lt getApplicant.recordcount>,</cfif></cfoutput></cfif></cfloop>];
Try this
var address = [<cfset i=0><cfloop query="getApplicant"><cfset i=i+1><cfif getApplicant.dbHomePostCode GT ""><cfoutput>'#getApplicant.dbHomePostCode#'<cfif i LT getApplicant.RecordCount>,</cfif></cfoutput></cfif></cfloop>];
The above code uses an Integer(i) to store the position in the loop, and when it es to output the ma checks to see if i is Less Than the size of the SQL result. This way it will only output a ma if it is not the last row of the result-set.
Using query.recordcount you can determine if you are at the last line of your query and adjust the output accordingly. Overall Tomalak and Ben Doom have excellent answers, but this one shows how you could have fixed the issue using your original line of thinking.
<cfset var address = "[">
<cfloop query="getApplicant">
<!--- If not the last row, include the ma --->
<cfif getApplicant.dbHomePostCode NEQ "" AND getApplicant.currentrow NEQ getApplicant.recordcount>
<cfset address = address & getApplicant.dbHomePostCode & ",">
<!--- If last row, omit the ma --->
<cfelseif getApplicant.dbHomePostCode NEQ "">
<cfset address = address & getApplicant.dbHomePostCode>
</cfif>
</cfloop>
<cfset address = address & "];">
<!--- Now we output the string all at once --->
<cfoutput>#address#</cfoutput>
@Tomalok has a good answer. That's probably what I would use.
Before seeing his answer, here's what my answer probably would have been:
<cfset codelist = "">
<cfloop query="getApplicant">
<cfif len(dbHomePostCode)>
<cfset codelist = listappend("'#codelist#'", dbHomePostCode)>
</cfif>
</cfloop>
<cfset address = "[#codelist#]">