473,378 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Creating a unique order number using auto increment.

44
Hi,

I have had some good fortune on this site so I am back and I must iterate I am a beginer.

I am having some problems getting to grips with the right technique to manage variables and adding a unique order number to a customer order form.

I have 4 stages to my order form.

Stage 1 involves the user selecting a link based on what they want. The link then sets 2 variables. $type and $fault based on the link they have chosen.

In stage 2 I have used

[PHP]
<?php
$type = $_GET['$type'];
$fault = $_GET['$fault'];
?>

[/PHP]


and echo these details on the order form (in a read only input box to make sending them on easier, well for me anyway) and this works fine.

Would I still need to do this if I used sessions to handle my variables.

In stage 2 the user then has to fill in their details on a form and submit the from. I have used post as the action.

In stage 3 all the details are displayed correctly.

It is here where my problem really exists.

I want to add a unique order number, I have done this for the first order but cant get the order number to then auto increment. I have all the tables set up and am connecting to them fine.

The code for this is as follows

[PHP] <?php
$sql="SELECT * FROM i_counter"; // This table manages the incrementing counter. It contains 1 row called cnt.
$result=mysql_query($sql,$db);
$row = mysql_fetch_array($result);
$cnt= $row["cnt"];
$cnt++;
$order_id="IPRD" . $cnt; //prefixes the order number with company ID

// Update counter for order id's

$sql = "UPDATE i_counter SET cnt = $cnt";
$result = mysql_query($sql);

// Add a new temp database entry

$sql = "INSERT INTO i_orders (id, ordernumber) VALUES ('', '$order_id')";
$result = mysql_query($sql);
?>
[/PHP]

The code is currently sitting at the start of my file just after 2 include statements, one for db connection and the other for the page header.

Though I will probably more on to sessions soon the issue I really need help with is the auto incremneting order number.

Many thanks
Sep 19 '07 #1
12 28373
badvoc
44
[quote=badvoc]// Update counter for order id's

$sql = "UPDATE i_counter SET cnt = $cnt";
$result = mysql_query($sql);

[/PHP]

I seem to have solved this by using insert into instead of update.

But thanks if you have stopped by to help.
Sep 19 '07 #2
kovik
1,044 Expert 1GB
Why would you use a separate table for a counter? Have you ever heard of AUTO_INCREMENT?
Sep 19 '07 #3
pbmods
5,821 Expert 4TB
Heya, Badvoc.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)

Please don't use the phrase 'Help!!' in your thread title in the future. It violates the, Posting Guidelines, makes more work for me and in general actually turns people *off* to your thread before they've even read it.
Sep 19 '07 #4
badvoc
44
Sorry about the title, I did try to add some detail but now see that it is annoying to see help in titles.

I'm sure I'll be back.

Thanks
Sep 19 '07 #5
badvoc
44
Why would you use a separate table for a counter? Have you ever heard of AUTO_INCREMENT?
Yeah I have the cnt set to auto incremant. i am using a seperate table as I couldn't get it to work so tried it as an option and it is still there.

I also want to store the actual order number which is prefixed with a 4 letters thus making it different to the id.

I suppose now its working I could just go back to the one table and use the id column and auto increment that..

Cheers
Sep 19 '07 #6
kovik
1,044 Expert 1GB
You could also use the regular table and get the latest id from that table, then continue your incrementing.
Sep 19 '07 #7
pbmods
5,821 Expert 4TB
Heya, Badvoc.

Consider separating your ID from your ID prefix.

For example, if you have to items with these IDs:
  • ffr002
  • mcv003

Consider separating the two parts:
  • id=2, prefix=ffr
  • id=3, prefix=mcv

You can easily format the 'human-readable' version of the id on the PHP side (or you can even index it as a separate field on the MySQL table itself). Plus, AUTO_INCREMENT just works, and you have a separate field dedicated to storing item prefixes, which makes searching faster.

You also may want to consider mapping a many-to-one relationship between the two data:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `items`( `itemid` INT ZEROFILL UNSIGNED ... AUTO_INCREMENT, `prefixid` INT UNSIGNED ... );
  2.  
  3. CREATE TABLE `prefixes`( `prefixid` INT UNSIGNED ... AUTO_INCREMENT, `prefix` CHAR(3) NOT NULL UNIQUE ... );
  4.  
Sep 19 '07 #8
badvoc
44
After messing about with my design I have lost the section where I generate my order number so I am starting again and using the one table, as suggested, with an auto incrementing column to generate the ID. I then have an order_number column that stores the full order number.

I am unsure of how to call the last id used and add one to it to create the next value. I know about the auto increment feature but as I am not using that number directly as an order number I am unsure of how to do it.

The format for my order number is ABCD0000ID

ABCD are the initials to be placed in front of the every order number.
0000 are 4 zeros I added as I stumbled accross the code by chance and thought I would add it.
ID is the auto incremented number from the table.

The way I see it is that I have to get the last known value for ID then increase it by one and include the new ID when creating the order number and re submit the new ID to the table. Is there an easier way it can be done using the auto increment to better use.

Cheers
Sep 20 '07 #9
badvoc
44
Right I seem to have done it again. I have sort out the auto increment an am using 1 table.


The issue i have is with my 4 zeros. I want the order number to be made up of 4 letters and 6 numbers, so the 4 zeros are included in the number not just added to it as I have done.

[PHP] $order_id="IPRD" .sprintf("%04d", $order_id) . $cnt;[/PHP]

This is what I have but am sure it was different to this yesterday.

Any ideas?

Thanks
Sep 20 '07 #10
badvoc
44
I have sorted this too.

I am on to another part so if help is needed I will start a new topic if needed.

Cheers
Sep 20 '07 #11
pbmods
5,821 Expert 4TB
Heya, Badvoc.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. while( ! isset($order_id[5] )
  2. {
  3.     $order_id = '0' . $order_id;
  4. }
  5.  
Sep 20 '07 #12
Right I seem to have done it again. I have sort out the auto increment an am using 1 table.


The issue i have is with my 4 zeros. I want the order number to be made up of 4 letters and 6 numbers, so the 4 zeros are included in the number not just added to it as I have done.

[PHP] $order_id="IPRD" .sprintf("%04d", $order_id) . $cnt;[/PHP]

This is what I have but am sure it was different to this yesterday.

Any ideas?

Thanks
Hi badvoc

Just happened to come across your question and is exactly what I am looking for would there be any chance you still have the code. Thanks
Jul 17 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: MrBoom | last post by:
Does using uniqid seem a reasonable way of generating a unique row identifyer in a db table? It's *highly* unlikely that two ids are going to be generated in the same microsecond, but if they are,...
3
by: margraft | last post by:
Hello, I'm somewhat of a newbie and I need to create a view with a column that is not derived from any other tables. What I want is for this field to be an auto-increment field or some kind of...
4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
7
by: Yannick Turgeon | last post by:
Hello all, I'm using SS2K on W2k. I'v got a table say, humm, "Orders" with two fields in the PK: OrderDate and CustomerID. I would like to add an "ID" column which would be auto-increment...
3
by: Chris | last post by:
Before I started to create table, etc to track unique form field record number assigments I thought I'd check to see if there is now a better way to do this in .NET. I have a parent form (table)...
13
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when...
3
by: crazy123 | last post by:
Please guide me as how do can we create a aut generated number... I have created a database. I am giving each name in the database entry a unique ID I want the Unique ID to be auto generated...
28
by: Matuag | last post by:
How can we create a unique and non-editable ID from client's first and last name? eg.: SmitJ1 for John Smith Matuag
5
by: Claire | last post by:
Hi, I can imagine this question has been brought up before but I've spent all morning trying to find what I need on google without success. My application sits on Mysql or MS sql server engines...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.