Page 1 of 1

Split evenly

Posted: Wed Sep 15, 2004 5:44 pm
by ek5932
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:

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;
?>
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

Posted: Wed Sep 15, 2004 5:52 pm
by feyd

Code: Select all

SELECT COUNT( * ) num FROM users GROUP BY sector ORDER BY num
or similar...

then you can just look at the results, if they all match, use the random, otherwise, the first record is the one to use.

Posted: Wed Sep 15, 2004 6:01 pm
by ek5932
thanks but how do i make it random between two different sectors..

say if sector 3 and 6 are the equal then choose 3 or 6?

Posted: Wed Sep 15, 2004 6:17 pm
by feyd
add the sectors you want to choose from into an array, select a random element from that array.

Posted: Wed Sep 15, 2004 7:33 pm
by ek5932

Code: Select all

<?php
$result = mysql_query("SELECT COUNT( * ) num FROM users GROUP BY sector ORDER BY num");
$row = mysql_fetch_array($result);
echo $row[0];
?>
Returns 1 when the first free sector is 4 i tried looking around for more information on count but couldn't find anything decent *kicks google*

Posted: Wed Sep 15, 2004 7:45 pm
by feyd
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.

Posted: Wed Sep 15, 2004 8:39 pm
by ek5932
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.

Posted: Wed Sep 15, 2004 9:07 pm
by dethron
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 :)

Posted: Wed Sep 15, 2004 9:08 pm
by John Cartwright
[php_man]array_rand[/php_man]

Posted: Wed Sep 15, 2004 9:09 pm
by dethron
and as Phenom stated above, it is very easy to select a random element of the given array ;)

Posted: Thu Sep 16, 2004 5:50 am
by ek5932
Okay now I know hot to set the random script I just don't know how to get the smallest sector if two are the same.

Posted: Thu Sep 16, 2004 6:10 am
by John Cartwright
What do you mean sector?

Posted: Thu Sep 16, 2004 6:27 am
by dethron
say you have and array that contains 2nd and 4th sector, because you know these two have the same amount of kingdoms (or users or whatever.....)

Code: Select all

<?php
//initializing
$sarr = ['2','4'];
?>
and you want to insert new user to 2nd sector, because its number is less than 4. Right?

The use following function

Code: Select all

<?php
function FindMin($arr){
		$cnt = count($arr);
		$tmp = $arr[0];
		for($i=1; $i<$cnt; $i++){
			if($arr[$i] < $arr[0]){
				$arr[0] = $arr[i];
				$arr[i] = $tmp;
				$tmp = $arr[0];
			}
		}
		return $arr;
	}
?>
Now, the least element of the array at the first position :)
And if you do not need the array anymore, you can simplify the function :)

Posted: Thu Sep 16, 2004 7:40 am
by ek5932
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.

Posted: Thu Sep 16, 2004 7:58 am
by dethron
so?
if there is one, then it is the smallest (bigger also)
if there is more than one, make an array and sent it to FindMin($arr)-func.

problem?