PHP generates unique promotion and offers and discount code of with source code

  • 2020-05-27 04:34:08
  • OfStack

Each e-commerce site now has one or more types of discount/discount/coupon systems. Share how to generate a 1-only promotion/discount code on PHP. It is mainly to implement a coupon code system, which can be used to track users from some specific sources. For example, when some hosts link to other pages for promotion, there will be coupon code generation, and more promotion codes. Therefore, today we will discuss the implementation of such a coupon code

Need for consideration
The code should be easy to remember, so it's a good idea to keep it short so 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 correct. There is no 1 standard length, because it depends on the length you want to generate, for example, if you want to generate 1000 code code, then you need at least 4 character code. The promotion/discount code is usually 4 to 8 characters long, but it depends on your request.
All right, let's get started! Let's look at the code, and then I'll explain it in detail. It is easy to
 
<?php 
/** 
* @param int $no_of_codes// define 1 a int Type parameter   To determine how many coupon codes to generate  
* @param array $exclude_codes_array// define 1 a exclude_codes_array Array of type  
* @param int $code_length // define 1 a code_length To determine the length of the coupon code  
* @return array// Returns an array of  
*/ 
function generate_promotion_code($no_of_codes,$exclude_codes_array='',$code_length = 4) 
{ 
$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
$promotion_codes = array();// This array is used to receive the generated coupon 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 Numbers are no longer defined $promotion_codes Inside the function  
if(!in_array($code,$promotion_codes)) 
{ 
if(is_array($exclude_codes_array))// 
{ 
if(!in_array($code,$exclude_codes_array))// Exclude coupon codes that have been used  
{ 
$promotion_codes[$j] = $code; Assign the generated new coupon code to promotion_codes An array of  
} 
else 
{ 
$j--; 
} 
} 
else 
{ 
$promotion_codes[$j] = $code;// Assign the coupon code to the array  
} 
} 
else 
{ 
$j--; 
} 
} 
return $promotion_codes; 
} 
echo '<h1>Promotion / Discount Codes</h1>'; 
echo '<pre>'; 
print_r(generate_promotion_code(50,'',4)); 
echo '</pre>'; 
?> 

The code consists of three parameters,
The first parameter is the number of coupon codes you want to generate (in this case, 50). The second parameter, exclude array, ensures that only 1 coupon code is generated in the current list, so if you already have some unused code in the database, you can pass it to exclude. The last parameter is the length of the coupon code. This function will return the coupon code of the specified length and in this case, the 4-bit coupon code.

Here I've used a combination of Numbers and uppercase letters, assigned to the $characters string, and you can use lowercase letters or any other combination. This function generates a coupon code of only 1. This is the PHP version. Next time I will give you an NET version. I hope this can help you
Download address

Related articles: