PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have 5 sectors and when a user joins I want to put the user into the sector which has the least users if they are all the same then put them in a random sector. Heres the code I have:
<?php
$sector1 = @mysql_query("SELECT * FROM users WHERE sector='1'")or die (mysql_error());
$num1 = mysql_num_rows($sector1);
$sector2 = @mysql_query("SELECT * FROM users WHERE sector='2'")or die (mysql_error());
$num2 = mysql_num_rows($sector2);
$sector3 = @mysql_query("SELECT * FROM users WHERE sector='3'")or die (mysql_error());
$num3 = mysql_num_rows($sector3);
$sector4 = @mysql_query("SELECT * FROM users WHERE sector='4'")or die (mysql_error());
$num4 = mysql_num_rows($sector4);
$sector5 = @mysql_query("SELECT * FROM users WHERE sector='5'")or die (mysql_error());
$num5 = mysql_num_rows($sector5);
if($num1 < $num2 && $num3 && $num4 && $num5){$num = 1;}
if($num2 < $num1 && $num3 && $num4 && $num5){$num = 2;}
if($num3 < $num1 && $num2 && $num4 && $num5){$num = 3;}
if($num4 < $num1 && $num2 && $num3 && $num5){$num = 4;}
if($num5 < $num1 && $num2 && $num3 && $num4){$num = 5;}
if(!$num){$num = rand(1,5);}
$sector = $num;
?>
Problem is if two sectors have the same amount of kingdoms then it will just insert the user into a random sector. I need to insert the user into the sector with the lowest users and if there is a few sectors with no kingdoms then the user is inserted to one of them randomly and not all of them.
Any help would be greatly apreciated its doing my head in. thanks
you might want to notice that it only returns the number found... you might want to add to it to select the sector, so you know which one you are dealing with.
just just the selecting part, lol now I am back to where i satrted the problem was im not sure how to choose the lowest sector or if two are even to choose randomly between them two.
why dont you add them to a array, then choose a random element of this array to insert the user
of course, array elements are sector numbers which is equal
all that code does it choose the random number, I already had that.
I have the script to find how many users are in each sector all I need is to select the sector with the least users and if there are two then use the array.