Split evenly

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!

Moderator: General Moderators

Post Reply
ek5932
Forum Commoner
Posts: 25
Joined: Mon Jun 28, 2004 5:55 pm

Split evenly

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ek5932
Forum Commoner
Posts: 25
Joined: Mon Jun 28, 2004 5:55 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

add the sectors you want to choose from into an array, select a random element from that array.
ek5932
Forum Commoner
Posts: 25
Joined: Mon Jun 28, 2004 5:55 pm

Post 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*
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
ek5932
Forum Commoner
Posts: 25
Joined: Mon Jun 28, 2004 5:55 pm

Post 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.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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 :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

[php_man]array_rand[/php_man]
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

and as Phenom stated above, it is very easy to select a random element of the given array ;)
ek5932
Forum Commoner
Posts: 25
Joined: Mon Jun 28, 2004 5:55 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What do you mean sector?
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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 :)
ek5932
Forum Commoner
Posts: 25
Joined: Mon Jun 28, 2004 5:55 pm

Post 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.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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?
Post Reply