Hello,
I am trying to automatically generate 6 digit numbers in a Coldfusion Application.
When a user clicks to add a new product, I want the generated numbers to be in the form when the product information is being entered.The problem is that the database already contains numbers for some of the products so I have to make sure that the number generated is not equal to any of the numbers in the Database.
Any suggestions on how to go about this?
I found the code below and kinda changed it to generate six numbers but how do I make sure that every number generated is diffrerent from the next and not equal to any of the numbers in the database and doesn't begin with a zero?
<cffunction
name="nowToNumber"
access="private"
returntype="numeric"
output="false"
hint="This function generates a 6-digit unique number from the current date and time">
<cfparam name="result" default="0">
<!---
get the year, month, day, hour, minute, and second from the current date/time and store them in variables
--->
<cfset vYear=year(now())>
<cfset vMonth=month(now())>
<cfset vDay=day(now())>
<cfset vHour=hour(now())>
<cfset vMinute=minute(now())>
<cfset vSecond=second(now())>
<!---
check the length of the month, day, hour, minute, and second. if the length is less than 0 then store "00" into the variables, if the
length is 1 then append "0" to the beginning of the variable value. this is to ensure that the generated number is 14-digits.
--->
<!---<cfif len(vMonth) EQ 0>
<cfset vMonth="00">
<cfelseif len(vMonth) EQ 1>
<cfset vMonth="0" & vMonth>
</cfif>
<cfif len(vDay) EQ 0>
<cfset vDay="00">
<cfelseif len(vDay) EQ 1>
<cfset vDay="0" & vDay>
</cfif>--->
<cfif len(vHour) EQ 0>
<cfset vHour="00">
<cfelseif len(vHour) EQ 1>
<cfset vHour="0" & vHour>
</cfif>
<cfif len(vMinute) EQ 0>
<cfset vMinute="00">
<cfelseif len(vMinute) EQ 1>
<cfset vMinute="0" & vMinute>
</cfif>
<cfif len(vSecond) EQ 0>
<cfset vSecond="00">
<cfelseif len(vSecond) EQ 1>
<cfset vSecond="0" & vSecond>
</cfif>
<!---
concatenate the variables in the format "YYYYMMDDHHMMSS" and return the result.
--->
<cfset result = <!---vYear & vMonth & vDay &---> vHour & vMinute & vSecond>
<cfreturn result>
</cffunction>
<cfoutput>#nowToNumber()#</cfoutput>
Any suggestions / ideas will be greatly appreciated.
Thanks
I am trying to automatically generate 6 digit numbers in a Coldfusion Application.
When a user clicks to add a new product, I want the generated numbers to be in the form when the product information is being entered.The problem is that the database already contains numbers for some of the products so I have to make sure that the number generated is not equal to any of the numbers in the Database.
Any suggestions on how to go about this?
I found the code below and kinda changed it to generate six numbers but how do I make sure that every number generated is diffrerent from the next and not equal to any of the numbers in the database and doesn't begin with a zero?
<cffunction
name="nowToNumber"
access="private"
returntype="numeric"
output="false"
hint="This function generates a 6-digit unique number from the current date and time">
<cfparam name="result" default="0">
<!---
get the year, month, day, hour, minute, and second from the current date/time and store them in variables
--->
<cfset vYear=year(now())>
<cfset vMonth=month(now())>
<cfset vDay=day(now())>
<cfset vHour=hour(now())>
<cfset vMinute=minute(now())>
<cfset vSecond=second(now())>
<!---
check the length of the month, day, hour, minute, and second. if the length is less than 0 then store "00" into the variables, if the
length is 1 then append "0" to the beginning of the variable value. this is to ensure that the generated number is 14-digits.
--->
<!---<cfif len(vMonth) EQ 0>
<cfset vMonth="00">
<cfelseif len(vMonth) EQ 1>
<cfset vMonth="0" & vMonth>
</cfif>
<cfif len(vDay) EQ 0>
<cfset vDay="00">
<cfelseif len(vDay) EQ 1>
<cfset vDay="0" & vDay>
</cfif>--->
<cfif len(vHour) EQ 0>
<cfset vHour="00">
<cfelseif len(vHour) EQ 1>
<cfset vHour="0" & vHour>
</cfif>
<cfif len(vMinute) EQ 0>
<cfset vMinute="00">
<cfelseif len(vMinute) EQ 1>
<cfset vMinute="0" & vMinute>
</cfif>
<cfif len(vSecond) EQ 0>
<cfset vSecond="00">
<cfelseif len(vSecond) EQ 1>
<cfset vSecond="0" & vSecond>
</cfif>
<!---
concatenate the variables in the format "YYYYMMDDHHMMSS" and return the result.
--->
<cfset result = <!---vYear & vMonth & vDay &---> vHour & vMinute & vSecond>
<cfreturn result>
</cffunction>
<cfoutput>#nowToNumber()#</cfoutput>
Any suggestions / ideas will be greatly appreciated.
Thanks