Home  >  Article  >  Backend Development  >  How to generate unique order numbers in php

How to generate unique order numbers in php

藏色散人
藏色散人Original
2020-11-17 09:27:464819browse

php method to generate unique order numbers: 1. Use the database primary key value to generate a self-increasing order number; 2. Implement the order number of "date self-increasing number"; 3. Generate a random order number ; 4. Implement "alphanumeric string type" order number.

How to generate unique order numbers in php

Recommended: "PHP Video Tutorial"

Summary of methods for PHP to generate unique order numbers

//商品编号生成
(YmdHi)拼接上兑换表生成的Id
//订单号的生成
(YmdHi)拼接上订单表生成的Id
echo date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);

Several common ways of order numbers:

1. Use the database primary key value to generate a self-increasing order number (the order number is the primary key of the data table )

2. Order number with self-increasing date number (for example: 2012040110235662)

3. Generate a random order number (65865325365966)

4. Alphanumeric string format , letters have special meanings, C02356652

Order number design principles: Design on demand

The unique feature code used to retrieve order details can be retrieved using the order number Including order date, product category, color, size (or style), warehouse and other information, the order number containing too much information is a bit "superfluous"! Just design it as you want!

User experience rules for order number design:

1. No duplication of order numbers;

2. If it is convenient for customer service, it is best to " With the order number in the format of "Date Auto-increment", customer service will know at a glance whether the order is within the return guarantee period;

3. Keep the length of the order number as short as possible (within 10 digits), which is convenient for users, especially when making complaints over the phone. , long numbers have a high probability of error reporting, which affects customer service efficiency;

4. Try to keep the order number in numeric type (pure integer). In the database order index query, the data indexing and retrieval efficiency of long integer numeric type is far less efficient. Much higher than text, so try to avoid "alphanumeric string"!

It is often necessary to generate a unique order number when doing shopping mall projects. Here is a summary!

Method 1:

return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);

Method 2: In use. . .

return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);

Method 3: Used before. . .

public function make_order($user_id)
{
return mt_rand(10,99)
. sprintf('%010d',time() - 946656000)
. sprintf('%03d', (float) microtime() * 1000)
. sprintf('%03d', (int) $user_id % 1000);
}

Method 4:

$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
$orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));

The above is the detailed content of How to generate unique order numbers in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn