php – 什么是生成大量独特促销代码的最佳方式?
发布时间:2020-12-13 21:39:58 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试大批量创建促销代码(使用php / mysql). 目前我的代码看起来像这样: $CurrentCodesInMyDB = "asdfasdf,asdfsdfx"; // this is a giant comma delimited string of the current promo codes in the database.$PromoCodes = "";for($i=0;$i=30000;$i
我正在尝试大批量创建促销代码(使用php /
mysql).
目前我的代码看起来像这样: $CurrentCodesInMyDB = "asdfasdf,asdfsdfx"; // this is a giant comma delimited string of the current promo codes in the database. $PromoCodes = ""; for($i=0;$i<=30000;$i++) { $NewCode = GetNewCode($PromoCodes,$CurrentCodesInMyDB ); $PromoCodes .= $NewCode . ","; //this string gets used to allow them to download a txt file //insert $newcode into database here } function GetNewCode($CurrentList,$ExistingList) { $NewPromo = GetRandomString(); if(strpos($CurrentList,$NewPromo) === false && strpos($ExistingList,$NewPromo) === false) { return $NewPromo; } else { return GetNewCode($CurrentList,$ExistingList); } } function GetRandomString() { return "xc34cv87"; //some random 8 character alphanumeric string } 当我在10k批量生产时,似乎没问题.但客户希望能够一次产生30k.当我将循环撞到30k时,我一直遇到问题.我可以做出任何明显的性能调整,或者我可以采用不同的方式吗? 解决方法
您可能不需要在一个巨大的字符串中将所有30,000个代码加载到内存中.只需在数据库中创建一个表,添加代码唯一字段(主键或唯一索引)并插入随机代码,直到您有30,000个成功插入.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |