Copy link to clipboard
Copied
I am using zipfinder.cfc to find out places local to certain zip codes. For some reason, it works perfectly fine with some zip codes but for others it returns the local zip codes with the first digit removed. When I search for 20002 the results seem to all work perfectly. When i test it with 08826 nearly none of the zip codes work. For instance, the first result is 7001 when in actuality it should be 07001. Any idea why this would happen? Whatever is causing this is really destroying any reliability of my results.
When I search for "20002" as a zip code here are my result (local zipcodes)
'20002','20003','20017','
I will make one side note, for some reason I was constantly getting mysql errors for using "SQUARE" in my query. I removed both of the references and just multipled each equation by itself, effectively squaring them. I dont know if the square function was removed in my version of mysql or what but I dont think that could possibly be the cause of this because they are being squared just by being multipled by each other.
Any help would be greatly appreciated!! Thanks in advance!
mark
Copy link to clipboard
Copied
How are you parsing the list of zip codes? Computers will not keep a leading zero on any value in a numeric variable. You must make sure the variable is considered a string so that it keeps the leading zero.
Copy link to clipboard
Copied
This is how I am importing the results into my application:
<cfinvoke component="zipfinder" method="squareSearch" radius="25"
zip="08826" returnvariable="results"></cfinvoke>
<cfquery name="get_resellers" datasource="TEST">
select *
from PartnerView
where zipcode IN (#ListQualify(ValueList(results.zip),"'")#)
and Country = 'USA'
order by CompanyName
</cfquery>
Here is the full zipfinder.cfc code. Zip is set as a string in the first line (from what I can tell). I understand what you are saying and that definitely seems to be the problem, but where does Zip stop being a string? I appreciate the help!
<cfcomponent>
<cffunction name="zipLocation" access="public" hint="Given a zip code, it will look up the city and state">
<cfargument name="zip" type="string" required="true">
<cfquery name="qGetZip" datasource="sc">
select city,state
from zipcode
where zipcode = <cfqueryparam value="#zip#" cfsqltype="CF_SQL_CHAR">
</cfquery>
<cfreturn qGetZip>
</cffunction>
<cffunction name="zipToLL" access="public">
<!---
This is a helper function. Given a zip code, it will look up the
relevant lats and longs (and their corresponding values in radians)
and then pass back a structure containing this information --->
<cfargument name="zip" type="string" required="true">
<!--- gets a new coordinate pair --->
<cfset cp = getNewCoordinate()>
<cfquery name="qGetLL" datasource="sc">
select latitude, longitude
from zipcode
where zipcode = <cfqueryparam value="#zip#" cfsqltype="CF_SQL_CHAR">
and latitude IS NOT NULL <!--- tpullis fix --->
</cfquery>
<cfif qGetLL.recordcount gt 0>
<cfset cp.latitude = qGetLL.latitude>
<cfset cp.longitude = qGetLL.longitude>
<cfset cp.rlatitude = 0> <!--- qGetLL.rlatitude> --->
<cfset cp.rlongitude = 0> <!--- qGetLL.rlongitude> --->
</cfif>
<cfreturn cp>
</cffunction>
<!--- +++++++++++++++++++++++++++++++++++++++++++++ --->
<cffunction name="getNewCoordinate" access="public">
<!---
Basically, this is a constructor that gives us a blank coordinate pair.
--->
<cfset retVal = structNew()>
<cfset retVal.latitude = 0>
<cfset retVal.longitude = 0>
<cfset retVal.rlatitude = 0>
<cfset retVal.rlongitude = 0>
<cfreturn retVal>
</cffunction>
<!--- +++++++++++++++++++++++++++++++++++++++++++++ --->
<cffunction name="squareSearch" access="public">
<!---
This function performs a proximity search by building out a rectangle
from a given set of coordinates, and then returning matching items that
fall within that area. It is not the most accurate way to search, but
for smaller distances, it is okay. It is also very fast.
--->
<cfargument name="radius" type="numeric" required="true">
<cfargument name="zip" type="string" required="true">
<cfset radius = arguments.radius>
<cfset zip = arguments.zip>
<cfset z1 = zipToLL(zip)>
<cfset lat_miles = 69.1> <!--- You can change this if you need more precision --->
<cfset lon_miles = abs(lat_miles * cos(z1.latitude * 0.0174))>
<cfset lat_degrees = radius / lat_miles>
<cfset lon_degrees = radius / lon_miles>
<!--- This is where we calculate the bounds of the search rectangle --->
<cfset lat1 = z1.latitude - lat_degrees>
<cfset lat2 = z1.latitude + lat_degrees>
<cfset lon1 = z1.longitude - lon_degrees>
<cfset lon2 = z1.longitude + lon_degrees>
<!---
To perform the search, we're going to use trigonometry. Remember the equation, x^2 + y^2 = z^2,
aka the Pythazizzle Thizzle? If you look closely, you can see that we are using that in order
to calculate the distance (dist) in the query below.
This is good, because it is a fast calculation. But, it is bad because it is calculating the
distance as if it were a line. If the world were flat, this would be perfect. But, since it isn't,
this will start to show errors the larger the radius gets.
Still, for your applications, the errors might be small enough to justify the BLAZING SPEED.
--->
<cfquery name="qSquareSearch" datasource="sc">
select zipcode as zip, state, city,
SQRT(SQUARE(#lat_miles# * (latitude - (#z1.latitude#)))
+
square(#lon_miles# * (longitude - (#z1.longitude#)))
) as dist
from zipcode
where latitude between #lat1# AND #lat2#
AND longitude between #lon1# AND #lon2#
order by dist asc
</cfquery>
<!---
This is just a quick filter query that will remove some of the zips that get erroneously
included in the result set. This helps to offset the errors that this method introduces,
but only just a little.
--->
<cfquery name="qRefine" dbtype="query">
select * from qSquareSearch where dist < <cfqueryparam value="#radius#" cfsqltype="CF_SQL_INTEGER">
</cfquery>
<cfreturn qRefine>
</cffunction>
</cffunction>
</cfcomponent>
Copy link to clipboard
Copied
I'm sorry, I do not see any obvious line that jumps out as one that is converting the value. This is an unfortunate side affect of the typless nature of ColdFusion. I can only sugest steps to debug the problem. Track down where the value gets turned into a number. At the first point the zip value is used put a <cfoutput>#scope.zip#</cfoutput><cfabort> See if this outputs "0zzzz" or just "zzzz". If it does move the output-break to the next point it is used.
There are some tricks that can force ColdFusion to sometimes use a specific data type that maybe able to help if you can find where it is being converted.
Copy link to clipboard
Copied
Ok thank you I appreciate the help! I will test that as soon as I get home today and report back my finding and if I have found a solution!
Copy link to clipboard
Copied
Just wanted to follow up, I figured out the issue. I had my database field value set to 'int' which cause them to be truncated within the database. So I changed the value to text and reuploaded all of my data and I am good as new!
Thanks so much for your help you definitely opened up my eyes and let me figure it out!