Each e-commerce sites, and now there are one or more types of preferential discount/coupon system, how to share with you in the PHP generate a unique sales promotion/discount code. Mainly is to achieve a promo code system, can be used to track users from certain sources, for example, some host promotional links to other pages will have a promo code generation, there are more promo codes, etc. So today will discuss the implementation of such a promo code

to consider the needs of the
<? PHP/

* * * @ param int $no_of_codes//define an int type of parameters used to determine how many coupon codes generated
* @ param array $exclude_codes_array//define a exclude_codes_array an array of type
* @ param int $code_length//define a code_length parameters to determine the length of the promo code
* @ the return array//return the 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 promo code generated
the for ($j=0; $j <$no_of_codes; $j++)
{
$code="";
the for ($I=0; $I <$code_length; $i++)
{$code.
=$characters [mt_rand (0, strlen ($characters) - 1));

}//if the generated four random number is no longer we defined $promotion_codes function inside the
if (! In_array ($code, $promotion_codes))
{
if (is_array ($exclude_codes_array))//
{
if (! In_array ($code, $exclude_codes_array))//has ruled out using the coupon code in the
{
$promotion_codes [$j]=$code; Will generate new promo code assigned to promotion_codes array

} else
{
$j --;

}}
the else
{
$promotion_codes [$j]=$code;//the promo code assigned to the array

}}
the else
{
$j --;

}}
return $promotion_codes;
}
echo '<H1 >Promotion/Discount Codes echo '<Pre> ';
print_r (generate_promotion_code (50, "', 4));
echo '</PRE> ';
? >

the code consists of three parameters,
the first parameter is the number do you want to generate promo code (in this case is to generate 50). Exclude the second parameter array, ensure that in the current list is generated only coupon codes, 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 promo code. This function returns the specified length of the coupon code here is the coupon code of four.

I've been here, using a combination of uppercase letters and Numbers assigned to $characters in the string, you can use lowercase letters or any other letter combinations to try. The role of this function is to generate only one coupon code. The PHP version, next to a.net version, hope I can help you download address.


This concludes the body part