|
-
Aug 6th, 2009, 04:48 PM
#1
Thread Starter
Hyperactive Member
Basic PHP <> question
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.
Last edited by k0zz; Aug 6th, 2009 at 04:52 PM.
"Remember, remember the 5th of November, the gun powder treason and plot. I know of no reason why the gun powder treason should ever be forgot."
-
Aug 6th, 2009, 05:05 PM
#2
Re: Basic PHP <> question
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.
-
Aug 6th, 2009, 05:46 PM
#3
Re: Basic PHP <> question
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.
Last edited by SambaNeko; Aug 6th, 2009 at 05:53 PM.
-
Aug 9th, 2009, 12:07 PM
#4
Re: Basic PHP <> question
Your ranges vary, so I would recommend an array that you would use as a type of lookup:
PHP Code:
$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.
-
Aug 10th, 2009, 09:38 AM
#5
Junior Member
Re: Basic PHP <> question
PHP Code:
$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>";
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|