Home  >  Article  >  php教程  >  php生成 优惠券 激活码

php生成 优惠券 激活码

WBOY
WBOYOriginal
2016-06-06 19:52:411901browse

/* * * 生成vip激活码 * @param int $nums 生成多少个优惠码 * @param array $exist_array 排除指定数组中的优惠码 * @param int $code_length 生成优惠码的长度 * @param int $prefix 生成指定前缀 * @return array 返回优惠码数组 */ public function gene

<span>/*</span><span>*
     * 生成vip激活码
     * @param int $nums             生成多少个优惠码
     * @param array $exist_array     排除指定数组中的优惠码
     * @param int $code_length         生成优惠码的长度
     * @param int $prefix              生成指定前缀
     * @return array                 返回优惠码数组
     </span><span>*/</span>
    <span>public</span> <span>function</span> generateCode( <span>$nums</span>,<span>$exist_array</span>='',<span>$code_length</span> = 6,<span>$prefix</span> = ''<span> ) {
    
        </span><span>$characters</span> = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz"<span>;
        </span><span>$promotion_codes</span> = <span>array</span>();<span>//</span><span>这个数组用来接收生成的优惠码</span>
    
        <span>for</span>(<span>$j</span> = 0 ; <span>$j</span> $nums; <span>$j</span>++<span>) {
                
            </span><span>$code</span> = ''<span>;
                
            </span><span>for</span> (<span>$i</span> = 0; <span>$i</span> $code_length; <span>$i</span>++<span>) {
    
                </span><span>$code</span> .= <span>$characters</span>[<span>mt_rand</span>(0, <span>strlen</span>(<span>$characters</span>)-1<span>)];
    
            }
                
            </span><span>//</span><span>如果生成的4位随机数不再我们定义的$promotion_codes数组里面</span>
            <span>if</span>( !<span>in_array</span>(<span>$code</span>,<span>$promotion_codes</span><span>) ) {
    
                </span><span>if</span>( <span>is_array</span>(<span>$exist_array</span><span>) ) {
                        
                    </span><span>if</span>( !<span>in_array</span>(<span>$code</span>,<span>$exist_array</span>) ) {<span>//</span><span>排除已经使用的优惠码</span>
    
                        <span>$promotion_codes</span>[<span>$j</span>] = <span>$prefix</span>.<span>$code</span>; <span>//</span><span>将生成的新优惠码赋值给promotion_codes数组</span>
<span>    
                    } </span><span>else</span><span> {
    
                        </span><span>$j</span>--<span>;
    
                    }
                        
                } </span><span>else</span><span> {
                        
                    </span><span>$promotion_codes</span>[<span>$j</span>] = <span>$prefix</span>.<span>$code</span>;<span>//</span><span>将优惠码赋值给数组</span>
<span>                        
                }
    
            } </span><span>else</span><span> {
                </span><span>$j</span>--<span>;
            }
        }
    
        </span><span>return</span> <span>$promotion_codes</span><span>;
    }</span>

 上述方法可以直接拷贝使用

 

 下面列出个人优惠券激活码的数据表设计,各位看官感觉是否有用

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `vip_card`
-- ----------------------------
DROP TABLE IF EXISTS `vip_card`;
CREATE TABLE `vip_card` (
  `id` int(11) NOT NULL,
  `code` char(10) DEFAULT '' COMMENT '激活码',
  `bid` int(11) DEFAULT '0' COMMENT '购买人',
  `uid` int(11) DEFAULT '0' COMMENT '使用人',
  `dlength` int(11) DEFAULT '0' COMMENT '时长',
  `act_date` date DEFAULT '0000-00-00' COMMENT '激活时间',
  `end_date` date DEFAULT '0000-00-00' COMMENT '结束时间',
  `exp_date` date DEFAULT '0000-00-00' COMMENT '过期时间[如果送激活码的话,使用这个字段,过期多少天不可激活]',
  `localcity` int(11) DEFAULT '0' COMMENT '城市code',
  `create_time` datetime DEFAULT '0000-00-00 00:00:00' COMMENT '创建时间',
  `is_use` int(5) DEFAULT NULL COMMENT '是否已经使用  0未使用  1已使用',
  PRIMARY KEY (`id`),
  KEY `index_code` (`code`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='激活码';

-- ----------------------------
-- Records of vip_card
-- ----------------------------

Statement:
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
Previous article:PHP输出缓冲控制Next article:Linux下安装php的扩展scws