Split evenly
Posted: Wed Sep 15, 2004 5:44 pm
Sector = A bunch of 20 kingdoms put together
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:
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
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:
Code: Select all
<?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;
?>Any help would be greatly apreciated its doing my head in. thanks