PHP generates unique promotions/offers/discount codes (with source code) _php Tips

Source: Internet
Author: User
Each ecommerce site now has one or more types of discount/discount/coupon systems to share with you how to generate a unique promotion/discount code in PHP. The main is to implement a discount code system, can be used to track users from certain sources, such as some host sales when the link to other pages will have preferential code generation, as well as more promotional code. Therefore, today we will discuss the implementation of such a discount code

needs to consider
Code should be easy to remember, so keeping a short length is a good idea so that users can easily remember it
No special characters! It should be an alphanumeric combination, because it will always be easier for users to remember
Length Promotion/discount code is correct. Without a standard length, because it depends on the length you want to generate, for example, if you want to generate code for code 1000, then you need at least 4 character codes. Promotional/discount codes are usually 4 to 8 characters long, but it depends on your requirements.
All right, let's get started! Let's take a look at the code and then I'll explain it in detail. It's easy
Copy Code code as follows:

<?php
/**
* @param int $no _of_codes//defines an int type of parameter to determine how many discount codes are generated
* @param array $exclude _codes_array//defines a 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";
The $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 generated 4-bit random number is no longer within 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 code
{
$promotion _codes[$j] = $code; Assign the generated new discount code to the Promotion_codes array
}
Else
{
$j--;
}
}
Else
{
$promotion _codes[$j] = $code;//To assign a discount code to an array
}
}
Else
{
$j--;
}
}
return $promotion _codes;
}
Echo ' Echo ' <pre> ';
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 (50 is generated here). The second parameter, exclude array, ensures that the unique preference code is generated in the current list, so if you have some unused code in the database, you can pass it to exclude. The last parameter is the length of the discount code. This function will return the specified length of the discount code here is the 4-bit discount code.

Here I've used a combination of numbers and uppercase letters to assign to the $characters string, and you can try it with lowercase letters or any other combination of letters. The purpose of this feature is to generate a unique discount code. This is the PHP version, the next time to a net version of, hoping to help everyone
Download Address

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.