vendredi 23 mars 2018

PHP generate unique code with specifications

It must be a 3 letter code, the first letter must be X,Y or Z and it must follow these format: XAA, XAB, XAC .... until XAW then XBA, XBB, XBC .... Until XBW then XCA, XCB..... etc.

When 'X' is done, then YAA, YAB, so on... When 'Y' is done ZAA, ZAB, so on...

The 2nd and 3rd letter of this code cannot be X,Y or Z.

I am fetching last generate code from the database.

Here is what I have so far.. Confused about final conditions:

$valid = str_split("ABCDEFGHIJKLMNOPQRSTUVW");

if($result->num_rows() < 1)
    $last_code = 'XAA';
else {
    $result = $result->row_array();
    $last_code = $result['employee_code'];
}

$arr = str_split($last_code);

$first  = $arr[0];
$second = $arr[1];
$third  = $arr[2];

if($first == 'X') {

} else if($second == 'Y') {

} else if($third == 'Z') {

}




Aucun commentaire:

Enregistrer un commentaire