Re: Creating alphanumeric id for a table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



aveev wrote:
> <?
>     function generate_id($num) {
>         $start_dig = 4;
>         $num_dig = strlen($num);
>     
>         $id = $num;
>         if($num_dig <= $start_dig) {
>             $num_zero = $start_dig - $num_dig;
>         
>             for($i=0;$i< $num_zero; $i++) {
>                 $id = '0' . $id;
>             }
>         }
>         $id = 'AAA' . $id;
>         return $id;
>     }
> 
>     $app_id = generate_id(1);
>   
> ?>

Your function can be reduced to a one-liner:

function generate_id($num) {
  return 'AAA' . str_pad(strval($num), 4, '0', STR_PAD_LEFT);
}

If the prefix is always 'AAA' you should consider to use a numeric ID
for your database and let your database generate the autoincrement id
and use this function just for display.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux