[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Coldfusion number generator using 4 digits, increment by 1

Posted on 2011-09-26
12
Medium Priority
?
562 Views
Last Modified: 2013-12-24
I need example of code that will generate a 4 digit number increment by 1.
0
Comment
Question by:smares32371
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
12 Comments
 
LVL 39

Expert Comment

by:gdemaria
ID: 36601284

<cfset theNumber = 1000>

   incredment it by one...

<cfset theNumber = theNumber + 1>


  I have a feeling you want something more than that though...
0
 

Author Comment

by:smares32371
ID: 36601895
That's true, in order for me to get a unique incremental number for every time the page is loaded, do I have to insert the number into db table in order to keep track of the count.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36602012
What is the number being used for? Just wondering if there's a reason you can't just use your database to generate the number ie (ms sql - identity, mySQL auto_increment, etc..). You could always pad the value on output so it's at leat 4 characters

     ie NumerFormat(yourNumber, "0000")
0
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 

Author Comment

by:smares32371
ID: 36602034
invoice number
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36602071
So far I don't see why you couldn't let your db generate the number. (In fact it's better not to roll your own when possible. As it's easy to create dupes.) Just pad the output as needed. If you prefer you could create a db view /or add a calculated column that does the padding automatically.

Which db type?
0
 

Author Comment

by:smares32371
ID: 36602119
I'm sorry it just has to be a 4 digit random number sorry for the incremental part of it.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36602125
Here's an example

Results
1, '00001', '2011-09-26 00:00:00', 'Test invoice A'
2, '00002', '2011-09-26 00:00:00', 'Test invoice B'
3, '00003', '2011-09-26 00:00:00', 'Test invoice C'

Sample Structure
<!--- sample table --->
<!--- note: You may want to plan ahead and use something bigger than SMALLINT --->
CREATE TABLE Invoices (
invoiceID smallint auto_increment primary key,
invoiceDate datetime,
Notes varchar(255)
)

<!--- add sample data --->
INSERT INTO Invoices (invoiceDate, Notes)
SELECT curDate(), 'Test invoice A' union all
SELECT curDate(), 'Test invoice B' union all
SELECT curDate(), 'Test invoice C'

<!--- create one time VIEW that includes "InvoiceNum" column padded to 5 zeros --->
CREATE VIEW vInvoices
AS
SELECT  InvoiceID, lpad(invoiceID, 5,'0') As InvoiceNum, invoiceDate, Notes
FROM   Invoices

<!--- display results --->
SELECT InvoiceID, InvoiceNum, InvoiceDate, Notes
FROM   vInvoices

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 36602133
> I'm sorry it just has to be a 4 digit random number sorry for the incremental part of it.

Oh, okay. Then ignore my last post
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 2000 total points
ID: 36602138
<cfset theNUmber = randRange(1000,9999)

will give you a random number
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36602182
You could use randRange() to generate a random number. Use NumberFormat to pad it to 4 characters.

             ie   <cfset randInvoiceNum = numberFormat(randRange(0, 9999), "0000")>

But you'll have to loop to ensure it wasn't used already.  Create a unique constraint on the table to prevent dupes. Then use a try/catch to detect a constraint violation (ie the number was already used). Keep looping until the INSERT succeeds.

<!--- Use unique constraint to prevent dupes --->
CREATE TABLE Invoices (
invoiceID smallint auto_increment primary key,
invoiceNum char(4) NOT NULL,
UNIQUE KEY ukInvoices_InvoiceNum (invoiceNum))
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36602188
> <cfset theNUmber = randRange(1000,9999)

Actually you can start at 0 or 1 and pad the results. So you don't skip the first 999 numbers.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 36602208
@smares32371 - Glad you found an easy solution.  I have a feeling the functions by category view might come in handy in the future ;-)
0

Featured Post

Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question