#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Posts
    1
    Rep Power
    0

    Retrieving Available Usernames


    Hello,

    I have users in a database and each has a unique "operating initial" which consists of 2 letters. One person may have the operating initial CB and someone else has ES. When a new user registers, I want them to be auto assigned an operating initial. I know how to auto increment from the most recent users, so say the newest user has the operating initials of AF, then I know how to make it choose AG next. However, I want to create a dropdown of available operating initials AA - ZZ. How can I do this so it skips operating initials currently in use? Thanks
  2. #2
  3. Confusing Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,916
    Rep Power
    9570
    Two ways:

    1. PHP
    Get into an array a list of all the initials used, generate another array of the 676 possible initials, and diff the two.

    For the diff, I imagine array_diff() would be quite inefficient, so I'd use an associative array:
    PHP Code:
    $initials range("AA""ZZ"); // pretty sure this works
    $initials array_combine($initials$initials); // initials as both key and value. or just as keys with array_flip

    /* for each initial used already as $i { */
        
    unset($initials[$i]);
    /* } */ 
    2. Database
    Make a table in the database containing just the 676 initials, then do an outer join to retrieve all initials not used.
    sql Code:
    SELECT i.initials
    FROM initials i
    LEFT JOIN users u ON i.initials = u.initials /* find users */
    WHERE u.id IS NULL /* exclude initials that matched a user */
    ORDER BY i.initials


    If you needed this often then I might go with the database solution, but I figure user registration is "infrequent" so I'd go for the PHP solution.
    Last edited by requinix; April 8th, 2016 at 08:06 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    47
    Rep Power
    25
    range('AA', 'ZZ') works in the sense that it doesn't throw an error, unfortunately the result is the same as range('A', 'Z')

    You need something like
    PHP Code:
    $a range('A','Z');
    foreach (
    $a as $x) {
        foreach (
    $a as $y) {
            
    $initials[] = "$x$y";
        }

    Comments on this post

    • requinix agrees
  6. #4
  7. Confusing Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,916
    Rep Power
    9570
    Originally Posted by Barand
    range('AA', 'ZZ') works in the sense that it doesn't throw an error, unfortunately the result is the same as range('A', 'Z')
    Blargh. I remembered there being something about range() with strings like AA and ZZ, and I thought the fact I remembered it meant it worked the way one would expect: same way as increment
    PHP Code:
    $a "AZ"; echo ++$a// BA 
    buuut I guess not. Maybe there was a feature request to make it work like that? Anyway.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,228
    Rep Power
    1230
    I'd use the increment approach instead of the range and nested foreach. After doing the increment, you'd want to verify that it's only 2 characters. Incrementing "ZZ" will become "AAA".
  10. #6
  11. Confusing Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    15,916
    Rep Power
    9570
    Could do that with a for loop
    PHP Code:
    for ($i "AA"$i != "AAA"$i++) { // note that $i <= "ZZ" won't work because "AAA" <= "ZZ" 
    but I think Barand's dual foreach loops would be faster.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Apr 2009
    Posts
    2,228
    Rep Power
    1230
    Originally Posted by requinix
    Could do that with a for loop
    PHP Code:
    for ($i "AA"$i != "AAA"$i++) { // note that $i <= "ZZ" won't work because "AAA" <= "ZZ" 
    but I think Barand's dual foreach loops would be faster.
    It might be faster, but it would probably be measured in the nano or micro seconds or even pico seconds especially considering the small number of values being looped over. It would need to be benchmarked to be sure. Premature optimization is almost always a waste of time.

    Also, given the goal of the OP's question, using a for loop like that doesn't make much sense.

    The increment approach is cleaner and more self documenting.
    Last edited by FishMonger; April 9th, 2016 at 10:49 PM.

IMN logo majestic logo threadwatch logo seochat tools logo