我想在大批量(使用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时,我遇到了问题。是否有任何明显的性能调整,或者我可以做到这一点?
什么样的“问题”你一直在数组键检查? – thetaiko 2011-01-19 15:50:35
为什么你将代码存储在一个巨大的字符串中,而不是一个数组? – Jens 2011-01-19 15:52:13
这些代码存储在一个包含索引的表格中。我在列表中保留了这个列表,以便我可以将一个逗号分隔列表写入一个文本文件。 – Jason 2011-01-19 16:07:10