【问题标题】:generating a unique no for coupon code 6 to 8 digit for any no of coupon [duplicate]为任何优惠券编号生成唯一的优惠券代码 6 至 8 位编号 [重复]
【发布时间】:2014-01-04 16:33:50
【问题描述】:
for($i=0,$ii=1;$i<$_POST['no_of_coupon']; $ii++) {
    $unique_code=uniqid();
    $category_unique_code = substr($unique_code,rand(0,strlen($unique_code) - 6),6);
    $i++;
    echo $category_unique_code;
}

$_POST['no_of_coupon'] 是没有。例如。如果用户想要 10 万个或更多优惠券代码,即$_POST['no_of_coupon'],所有代码都插入到数据库中并且代码是唯一的,但我尝试过上述方法,但它不是唯一的,然后我尝试了另一种方法

function gen_random($length=32)
{
    $final_rand='';
    for($i=0;$i< $length;$i++)
    {
        $final_rand .= rand(0,9);
    }
    return $final_rand;
}
for($i=0,$ii=1;$i<$_POST['no_of_coupon']; $ii++) {
            $unique_code=gen_random(6);
            $category_unique_code = substr($unique_code,rand(0,strlen($unique_code) - 6),6);
            $i++;
            echo $category_unique_code;
}

这种方法也不会生成唯一的优惠券代码,我只需要 6 到 8 位唯一的编号所以有人有任何想法生成唯一的编号,请告诉我

【问题讨论】:

    标签: php


    【解决方案1】:

    此代码将生成一个 8 位数的唯一优惠券代码:

    function getUniqueCouponCode ()
    {
        $filename = 'number.txt';
        if (file_exists($filename)) {
            $actual_number = file_get_contents($filename);
        } else {
            $actual_number = 1;
        }
        file_put_contents($filename, $actual_number + 1);
        return str_pad($actual_number, 8, '0', STR_PAD_LEFT);
    }
    

    【讨论】:

      【解决方案2】:
      Try this to create a 6 or 8 digit unique no:
          <?php 
              $date=date('y-m-dh:i:s'); 
              echo substr(md5($date),0,6);
          ?>
      

      【讨论】:

      • 不,它不会创建 6 或 8 位唯一编号。 md5() 函数将哈希作为 32 个字符的十六进制数字返回,因此它也可以返回字母。
      猜你喜欢
      • 2013-04-22
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      相关资源
      最近更新 更多