PHP generates unique promotion/offer/discount code (with source code)_PHP tutorial

WBOY
Release: 2016-07-21 15:13:47
Original
953 people have browsed it

Every e-commerce website now has one or more types of offer/discount/coupon systems, let me share with you how to generate unique promotion/discount codes in PHP. The main purpose is to implement a discount code system, which can be used to track users from certain sources. For example, when some hosts are on promotion, discount codes will be generated when linking to other pages, and there are more promotional codes. Therefore, today we will discuss the implementation process of such a discount code

Consider the needs
The code should be easy to remember, so it is a good idea to keep the length short so that the user can Remember it easily
with no special characters! It should be an alphanumeric combination as it will always be easier for the user to remember the
length of the promotion/discount code correctly. There is not a standard length as it depends on the length of the code you want to generate, for example if you want to generate a code of 1000 codes then you need to be at least 4 characters in the code. Promotion/offer code length is usually 4 to 8 characters, but it depends on your requirements.
Okay then, let’s get started! Let's take a look at the code and then I'll explain it in detail. It's easy

Copy the code The code is as follows:

/**
* @param int $no_of_codes//Define an int type parameter to determine how many discount codes to generate
* @param array $exclude_codes_array//Define an array of exclude_codes_array type
* @param int $ code_length //Define a code_length parameter to determine the length of the discount code
* @return array//Return array
*/
function generate_promotion_code($no_of_codes,$exclude_codes_array='',$code_length = 4)
{
$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$promotion_codes = array();//This array is used Receive the generated discount code
for($j = 0; $j < $no_of_codes; $j++)
{
$code = "";
for ($i = 0; $i < $code_length; $i++)
{
$code .= $characters[mt_rand(0, strlen($characters)-1)];
}
//If the generated 4 digits The random number is no longer in the $promotion_codes function we defined
if(!in_array($code,$promotion_codes))
{
if(is_array($exclude_codes_array))//
{
if(!in_array($code,$exclude_codes_array))//Exclude already used discount codes
{
$promotion_codes[$j] ​​= $code; assign the generated new discount code to the promotion_codes array
}
else
{
$j--;
}
}
else
{
$promotion_codes[$j] ​​= $code;//Add discount Assign codes to array
}
}
else
{
$j--;
}
}
return $promotion_codes;
}
echo '

Promotion / Discount Codes

';
echo '
'; 
print_r(generate_promotion_code(50,'',4));
echo '< /pre>';
?>

The code consists of three parameters.
The first parameter is the number of discount codes you want to generate (here it is to generate 50 indivual). The second parameter, exclude array, ensures that unique discount codes are generated in the current list, so if you already have some unused codes in the database, you can pass it to exclude. The last parameter is the length of the discount code. This function will return a discount code of specified length. Here is a 4-digit discount code.

Here I have used a combination of numbers and uppercase letters, assigned to the string of $characters, you can try using lowercase letters or any other combination of letters. What this feature does is generate a unique discount code. This is the PHP version, next time I will give you a NET version, I hope it can help everyone
Download address

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326424.htmlTechArticleEvery e-commerce website now has one or more types of offers/discounts/coupon systems for everyone Share how to generate unique promotion/discount codes in PHP. Mainly to achieve a...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template