views:

233

answers:

5

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 comma 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 comma 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!

A: 

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 comes to output the comma checks to see if i is Less Than the size of the SQL result. This way it will only output a comma if it is not the last row of the result-set.

Seidr
+1 - not sure if the CF is correct, but that is how I deal with separators in loops.
Sky Sanders
Better to use Marcos' version which uses currentrow and recordcount rather than add the extra code for the "i" count.
Stephen Moretti
+5  A: 
var 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 comments refer to this initial revision.

Tomalak
@tomalak, does listAppend have the same problem with empty values?
Antony
No, it does not. `ListAppend()` does not create empty values or trailing commas. In fact, even `ListQualify()` does not look at empty values. `ListQualify("1,,2,,3,", "'")` would return `"'1','2','3'"`. However, `ListQualify("1, ,2, ,3, ", "'")` would return `"'1',' ','2',' ','3',' '"` since spaces are not empty values.
Tomalak
@Antony: I have edited my answer in this regard. It's even more straightforward now.
Tomalak
A: 

@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#]">
Ben Doom
A: 

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>];
Stefano DiFabio
Stefano, this worked great
Simon Hume
Glad I could help!
Stefano DiFabio
A: 

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 comma --->
    <cfif getApplicant.dbHomePostCode NEQ "" AND getApplicant.currentrow NEQ getApplicant.recordcount>
        <cfset address = address & getApplicant.dbHomePostCode & ",">

    <!--- If last row, omit the comma --->
    <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>
Dan Sorensen