0
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'] is the no . For eg. if user want 1 lakh coupon code or more i.e. $_POST['no_of_coupon'], all the code is inserted into the the database and the code is unique but I have tried above method , but its not unique then i tried another method

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;
}

This method is also not generating unique coupon code , I just need 6 to 8 digit unique no So somebody have any idea to generate unique no , please tell me

sas
  • 2,563
  • 1
  • 20
  • 28
PHP_USER1
  • 628
  • 1
  • 14
  • 29

2 Answers2

0

This code will generate an 8 digit unique coupon code:

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);
}
evalarezo
  • 1,134
  • 7
  • 13
-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);
    ?>
santanu
  • 1
  • 1
  • No, it doesn't create a 6 or 8 digit unique no. `md5()` function returns the hash as a 32-character hexadecimal number, so it can return letters also. – evalarezo Jan 04 '14 at 17:51