Hello Everyone!

I am wondering how would you go about creating a user id that is a total of 11 char?

The user ID starts off with either a P or E depends on the account type,

All listing in the database must have id, the ids are formated in the formate of:
E-nnnnnnnnn or P-nnnnnnnnn NO to users name can be the same but both P and E could have the same number and this is because the P or E make it different

E-235567889
E-846568735
E-564651549
P-456745321
P-352589739

would this be hard to do and also would it be hard to make this into a exenal function() so that I can use it with a number of scripts

like when the system its self is adding a record then it needs also to add the id to the record for the Entity

I think I know how to make it a function already, just am not sure the best way to have it make the numbers and also check to make sure its not used already

I think the function would go like this:

tell func if its a E or P id needed
func creates number (9 char) then take that number and adds either the P- or E- to the start of the number, once it has the id it compares it to a table of ids if it finds the id in the table it starts again if it does not find the id then it returns the new id

Any advice on this would be great, thank ahead of time!

Sincerely,
Christopher

    If you are storing these ids in a relational database, you can use an auto-increment column for the number, and possibly make a primary key out of the combination of another column that stores a single character (i.e., P or E) and this auto-incremented column. When retrieving the key, you then combine and format with [man]sprintf/man or [man]printf/man.

      Yes that would be ok, but I would like a random number not 000000001 etc etc etc

        Yes that would be ok, but I would like a random number not 000000001 etc etc etc

        This seems to have come up quite often lately.

        You can use [man]mt_rand/man and concatenate it with the letter. Make the user id column unique. While there is a unique constraint violation when you insert the row, generate a new number and try and insert again.

          ok, I have just looked at the man to see how to use this and I am trying to figure out how to have it 9 digits long, the number can be from 000000001 to 999999999 as long as its random and not going 1 2 3 etc

          Thanks laserlight for your help!

          Christopher

            Try:

            <?php
            $user_id = sprintf("%'09d", mt_rand(1, 999999999));
            echo $user_id;
            ?>
              Write a Reply...