PHP generates a unique promotion/discount code (with source code) _ PHP Tutorial

Source: Internet
Author: User
PHP generates a unique promotion discount code (with the source code ). Every e-commerce website now has one or more types of discount coupon systems. we will share with you how to generate a unique discount code in PHP. It is mainly used to implement a single e-commerce website. now there is one or more types of discount/coupon systems to share with you how to generate a unique promotion/discount code in PHP. A promotional code system is used to track user sources. for example, a promotional code is generated when a host is linked to another page during promotions, and more promotional codes are available. Therefore, we will discuss the implementation process of such a discount code today.

Requirements
The code should be easy to remember, so it is a good idea to keep the length short so that users can easily remember it.
No special characters! It should be a combination of letters and numbers, because it will always be easier for users to remember
The length promotion/discount code is correct. There is no standard length, because it depends on the length you want to generate. for example, if you want to generate code 1000, you need at least 4 characters of code. The promotion/discount code is generally 4 to 8 characters in length, but it depends on your requirements.
Well, let's get started! Let's take a look at the code and I will explain it in detail. It is easy

The code is as follows:


/**
* @ Param int $ no_of_codes // defines an int type parameter to determine how many discount codes are generated.
* @ Param array $ exclude_codes_array // defines an array of the exclude_codes_array type.
* @ Param int $ code_length // defines a code_length parameter to determine the length of the discount code.
* @ Return array // returns an array.
*/
Function generate_promotion_code ($ no_of_codes, $ exclude_codes_array = '', $ code_length = 4)
{
$ Characters = "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
$ Promotion_codes = array (); // This array is used to 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 four-digit random number is no longer defined in the $ promotion_codes function
If (! In_array ($ code, $ promotion_codes ))
{
If (is_array ($ exclude_codes_array ))//
{
If (! In_array ($ code, $ exclude_codes_array) // exclude used discount codes
{
$ Promotion_codes [$ j] = $ code; assign the generated discount code to the promotion_codes array.
}
Else
{
$ J --;
}
}
Else
{
$ Promotion_codes [$ j] = $ code; // assign the discount code to the array
}
}
Else
{
$ J --;
}
}
Return $ promotion_codes;
}
Echo 'promotion/Discount Codes ';
Echo'

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


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

Here I have used a combination of numbers and upper-case letters and assigned a string to $ characters. you can use a combination of lower-case letters or any other letters for trial. This function is used to generate a unique discount code. This is a PHP version. next time, I will give it to a NET version, hoping to help you.

Bytes. It mainly implements...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.