Php create product code

teera
teera used Ask the Experts™
on
0001
0002
0003
qwrujd12
0004
ey67334
0005


I have a product code some product code it come from manufacturing some i need to create by my self if i create by my self I want for 4 digit format like 0001 0002 How I use php to create new one   it must check if it have   0005 then it create 0006  In product code some time it have product  ode from factory like ab48945940 but product code must be unique
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
To create a four digit string from a number you can use the PHP str_pad() function.

To ensure that a product code is unique you can define a column of a data base table with the UNIQUE attribute.  An attempt to insert a duplicate into the column will cause MySQL to throw error number 1062.   You can trap this with mySQL_Errno() and detect the non-unique condition.
check it
might it will provide u the expected result

$strlength =7 ;  // you provide the number of string as u required
echo createRandomKey($strlength);
            
            function createRandomKey($amount){
      $keyset  = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
      $genkey = "";
      for ($i=0; $i<$amount; $i++)
            $genkey .= substr($keyset, rand(0, strlen($keyset)-1), 1);
      return $genkey ;      
}

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial