input input - 1 year ago 66
PHP Question

Generate alphanumeric employee ID

I would like to generate an alphanumeric employee id in PHP e.g.

EP0001


Any ideas?

Answer Source

We are going to need more than this to help you but this is what I would do.

Assuming EP is the start of every alphanumeric employee id then

<?php
$number = getTotalEmployees();

$id = 'EP'.$number;

function getTotalEmployees() {
    $num = 'Get number of Employees from database';
    ++$num; // add 1;

    $len = strlen($num);
    for($i=$len; $i< 4; ++$i) {
        $num = '0'.$num;
    }
    return $num;
}
?>

UPDATE

Thanks to Trefex If you use MySQL, you could store the employee ID in a field and set the ZEROFILL attribute. That way you don't have to deal with the zeroes in PHP

<?php
function getTotalEmployees() {
    $num = 'Get number of Employees from database';
    ++$num; // add 1;
    return $num;
}
?>