如何检查while()是否无穷无尽?

So let me explain the concept short: A user get assigned a random name from a list. It checks if someone already has the name and if thats true it will run the name gen thing until it found a name that isn't already assignes.
So that works pretty well so far, the only problem I've got is that there is no way of checking if all the usernames from the list are assigned. Sure I could build in a counter and check if its run over like 1000 times, but that wouldn't be a clean solution and also not really fool proof. Ill show you what I mean:

    $foundCl = false;
    while($foundCl == false){ // Get Callname n stuff
        $Cl = getCallname(); // <- selects a random name from a list

        $lFoundCl = true;
        $sql = mysql_query("SELECT * FROM Board_Callnames WHERE TID='$TID'");
        while ($row = mysql_fetch_array($sql)){  // Checks if the name is already assigned
            if($row['Callname'] == $Cl['callname']){
                $lFoundCl = false;
            }
        }

        if($lFoundCl == true){ // Found a name that isnt assigned yet, assign it to the user, dont worry about all the values here and the Cl[color] stuff
            $foundCl = true;
            $Cln = $Cl['callname'];
            $Clc = $Cl['color'];
            $ClEntry = mysql_query("INSERT INTO Board_Callnames (TID, UID, Callname, Color, Flair) VALUES ('$TID', '$UID', '$Cln', '$Clc', '0')");
        } else {
            $foundCl = false;
        }
        /* What I need:
        if(this == endless){ break; }
        */
    }

getCallname:

 function getCallname()
{
$callnames = array(0 => "Alfa", 1 => "Bravo", 2 => "Charlie"); // etc....

$colors = array(0 => "default", 1 => "primary", 2 => "success", 3 => "info", 4 => "warning", 5 => "danger", 6 => "lila", 7 => "poison", 8 => "wine"); // NOT IMPORTANT FOR THIS

$ran_cl = rand(0, count($callnames)-1); // Selects random values
$ran_co = rand(0, count($colors)-1);

return array("callname" => $callnames[$ran_cl], "color" => $colors[$ran_co]); //Return these
}

The whole logic seems wrong.

Rather than generate a random item from a list of all item, then check if its assigned, you should generate a random item from a list of unassigned items, eg:

SELECT * FROM `itemlist` WHERE `assigned` = 0 LIMIT 1

One way to approach this is to build a list of available names by checking each of the candiates, and then selecting a random name from those which aren't taken yet. If the available list is empty, then you have run out.

You could check if you already have count($callnames) users in your database since it's the maximum possible number of names.

Instead of storing the possible "callnames" and "colors" in a PHP array, you might want to move them to the database as well and assign IDs to them.

You could then create a query that returns a random unassigned name ID (if available) or an empty result set (if no name is available)... for example like this:

SELECT cn.id FROM callnames cn
LEFT JOIN Board_Callnames bcn ON bcn.callname = cn.callname
WHERE bcn.callname IS NULL
ORDER BY RAND() LIMIT 1

Obviously if you use IDs for the name, you could want to change the Board_Callnames table so that it doesn't store strings but the name IDs... (and also change my query accordingly) but that depends on what you're trying to do.


I just realized that this could work without IDs as well. You would need a table with one column containing the available callnames. You could then use this query to get a random available callname:

SELECT cn.callname FROM callnames cn
LEFT JOIN Board_Callnames bcn ON bcn.callname = cn.callname
WHERE bcn.callname IS NULL
ORDER BY RAND() LIMIT 1