Jump to content

Incrementation Problem


k0z

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/169153-incrementation-problem/
Share on other sites

Hi

 

You could use a couple of arrays, one for the starting point, one for the ending points and another for the level, then loop round the until your points is between the first and 2nd and then use the array subscript to get the level name from the 3rd.

 

That said, most of the time you would be grabbing the points from a database table, and you could easily just have another table of point bands and title.

 

All the best

 

Keith

you could use an array or something... but because there is no real mathematical pattern... theres no easy formula or something to provide... you might just want to change the level recommendations so that you can...

 

there's an online game that has levels 1-99 and uses this formula for example

 

<?php

function experience($L) {
  $a=0;
  for($x=1; $x<$L; $x++) {
    $a += floor($x+300*pow(2, ($x/7)));
  }
  return floor($a/4);
}

for($L=1;$L<100;$L++) {
  echo 'Level '.$L.': '.experience($L).'<br />';
}
?>

you might be able to do something similar and make adjustments to make the point system more applicable for you

Hi

 

You could use a couple of arrays, one for the starting point, one for the ending points and another for the level, then loop round the until your points is between the first and 2nd and then use the array subscript to get the level name from the 3rd.

 

That said, most of the time you would be grabbing the points from a database table, and you could easily just have another table of point bands and title.

 

All the best

 

Keith

 

Seems like a valid idea, although I'm not sure how to go about coding it. Could I possibly get an example of this?

Hi

 

Crude example (its near bed time, I want some sleep) but gives you the basic idea.

 

$Points = 567;
$startPosArray = array(0,1000,2000,10000);
$endPosArray = array(999,1999,9999,19999);
$levelName = array("Level 1","Level 2","Level 3","Level4");

foreach($startposArray as $key => $startPos)
{
if ($Points >= $startPos and $Points <= $endPosArray[$key]) $Level = $levelName[$key];
}

 

Could be cleaned up by breaking out of the loop when a match is found, or instead of having 3 seperate arrays using an array of arrays.

 

All the best

 

Keith

You can actually use a single array if the values are contiguous -

 

<?php
$arr = array();
$arr[] = 0; // -999 = Level 1
$arr[] = 1000; // -1999 = Level 2
$arr[] = 2000; //  - 9999 = Level 3
$arr[] = 10000; //  - 19999 = Level 4
$arr[] = 20000; //  - 29999 = Level 5
$arr[] = 30000; //  - 39999 = Level 6
$arr[] = 40000; //  - 49999 = Level 7
$arr[] = 50000; //  - 74999 = Level 8
$arr[] = 75000; //  - 99999 = Level 9
$arr[] = 100000; //  - 124999 = Level 10
$arr[] = 125000; //  - 149999 = Level 11
$arr[] = 150000; //  - 199999 = Level 12
$arr[] = 200000; //  - 249999 = Level 13
$arr[] = 250000; //  - 299999 = Level 14
$arr[] = 300000; //  - 349999 = Level 15
$arr[] = 350000; //  - 399999 = Level 16
$arr[] = 400000; //  - 449999 = Level 17
$arr[] = 450000; //  - 499999 = Level 18
$arr[] = 500000; //  - 549999 = Level 19
$arr[] = 550000; //  - 599999 = Level 20
$arr[] = 600000; //  - 649999 = Level 21
$arr[] = 650000; //  - 699999 = Level 22
$arr[] = 700000; //  - 749999 = Level 23
$arr[] = 750000; //  - 799999 = Level 24
$arr[] = 800000; //  - 849999 = Level 25
$arr[] = 850000; //  - 899999 = Level 26
$arr[] = 900000; //  - 949999 = Level 27
$arr[] = 950000; //  - 999999 = Level 28

$value = (int)$_GET['value']; // get a value to test

$level = 0; // initialize variable
foreach($arr as $key => $val){
if($val > $value){
	break;
}
$level++;
}
echo $level;
?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.