PDA

Click to See Complete Forum and Search --> : Basic PHP <> question


k0zz
Aug 6th, 2009, 04:48 PM
Lets say I have a variable $number. This variable can equal anything from 1 to 10,000,000

Now I want to assign $number a level, based on the following incrementation:

0-999 = Level 1
1,000-1,999 = Level 2
2,000 - 9,999 = Level 3
10,000 - 19,999 = Level 4
20,000 - 29,999 = Level 5
30,000 - 39,999 = Level 6
40,000 - 49,999 = Level 7
50,000 - 74,999 = Level 8
75,000 - 99,999 = Level 9
100,000 - 124,999 = Level 10
125,000 - 149,999 = Level 11
150,000 - 199,999 = Level 12
200,000 - 249,999 = Level 13
250,000 - 299,999 = Level 14
300,000 - 349,999 = Level 15
350,000 - 399,999 = Level 16
400,000 - 449,999 = Level 17
450,000 - 499,999 = Level 18
500,000 - 549,999 = Level 19
550,000 - 599,999 = Level 20
600,000 - 649,999 = Level 21
650,000 - 699,999 = Level 22
700,000 - 749,999 = Level 23
750,000 - 799,999 = Level 24
800,000 - 849,999 = Level 25
850,000 - 899,999 = Level 26
900,000 - 949,999 = Level 27
950,000 - 999,999 = Level 28
etc. etc. etc
etc. etc .etc


Is there any easy way of doing this, so as to not repeat an If statement over and over again?

Thanks.

kfcSmitty
Aug 6th, 2009, 05:05 PM
I would probably just create parallel arrays. Then, with an if statement, you could figure out what index the # is in between and that would correspond to the level in the other array.

SambaNeko
Aug 6th, 2009, 05:46 PM
I would use some math...

$level = (int)(($number*$number_of_levels)/$high_num) + 1

Examples:

If $number is 950, then ...
$level = (int)((950*28)/999999) + 1
$level = 1

If $number is 910,000, then ...
$level = (int)((910000*28)/999999) + 1
$level = 26

Hmmmmmmm.... Not quite... I'm sure it can be refined to work though - maybe a better math whiz than me can figure it out faster.

visualAd
Aug 9th, 2009, 12:07 PM
Your ranges vary, so I would recommend an array that you would use as a type of lookup:

$levels = array(1 => 999, 2=> 1999, 3 => 9999 .... );


In order to find the level you would loop through the array and test if the number is higher than the value of that element. If it isn't, the level the current array index.

Teranoz
Aug 10th, 2009, 09:38 AM
$levelmin = Array();
$levelmin[] = 0;
$levelmin[] = 1000;
$levelmin[] = 2000;
$levelmin[] = 3000;
$levelmin[] = 4000;
//add until all lvls are done, and you can add more later

if($lvl>5) //whatever
{
echo "XP between ".$levelmin[$lvl-1]." and ".$levelmin[$lvl]-1."<br>";
}