Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number Generator in ColdFusion

Status
Not open for further replies.

newbieDev

Programmer
Jun 11, 2007
38
US
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



 
If you're trying to generate a unique six digit number for each record (with no duplicates), the you should definitely let the database do this itself. There is absolutely no good reason for trying to do this in CF. The database can do it faster, more efficiently, and it is guaranteed to never duplicate a number.


Hope This Helps!

ECAR
ECAR Technologies

"My work is a game, a very serious game." - M.C. Escher
 
Thank you Kevin and ECAR.
I will look into letting the DB do the work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top