Results 1 to 5 of 5

Thread: Basic PHP <> question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    448

    Resolved 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."

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    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.

  3. #3
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    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.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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(=> 9992=> 1999=> 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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    21

    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
  •  



Click Here to Expand Forum to Full Width