Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    elseif to switch

    I have a script with a bunch of elseif statements. This would probably work best with a switch statement.

    PHP Code:
    if (isset($_POST['get_random'])) {
        
    $number $_POST['random'];

        if (
    $number <= 10) {
            
    $generate rand(10,80);
            echo 
    $generate;
        }
        elseif (
    $number <= 20 && $number 10) {
            
    $generate rand(20,200);
            echo 
    $generate;
        }
        elseif (
    $number <= 30 && $number 20) {
            
    $generate rand(100,800);
            echo 
    $generate;
        }
        elseif (
    $number <= 40 && $number 30) {
            
    $generate rand(750,1800);
            echo 
    $generate;
        }
        elseif (
    $number <= 50 && $number 40) {
            
    $generate rand(700,3000);
            echo 
    $generate;
        }
        elseif (
    $number <= 60 && $number 50) {
            
    $generate rand(700,4500);
            echo 
    $generate;
        }
        elseif (
    $number <= 70 && $number 60) {
            
    $generate rand(1000,5000);
            echo 
    $generate;
        }
        elseif (
    $number <= 80 && $number 70) {
            
    $generate rand(1100,7000);
            echo 
    $generate;
        }
        elseif (
    $number <= 90 && $number 80) {
            
    $generate rand(1500,9000);
            echo 
    $generate;
        }
        elseif (
    $number <= 100 && $number 90) {
            
    $generate rand(2000,10000);
            echo 
    $generate;
        }
        elseif (
    $number 100) {
            echo 
    "Illegal level";
        }

    PHP Code:
    switch ($number) {
    case 
    0:
    $generate rand(10,80);
    echo 
    $generate;
    break;

    That's what I have so far. I just don't know how to write up the comparison in the if/elseif statements.

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    i'm a bit tired but try this

    PHP Code:
    if (isset($_POST['get_random'])) {
        
    $number $_POST['random']; 
    switch (
    $number) {
    case 
    0:
    $generate rand(10,80);
    echo 
    $generate;
    break;
    }
    }
        elseif (
    $number 100) {
            echo 
    "Illegal level";
        } 

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    if (isset($_POST['get_random'])) {
        
    $number $_POST['random'];

            
    $minlim = array(=> 10,
                                   
    20 => 20,
                                   
    30 => 100,
                                   
    40 => 750,
                                   
    50 => 700,
                                   
    60 => 700,
                                   
    70 => 1000,
                                   
    80 => 1100,
                                   
    90 => 1500,
                                   
    100 => 2000);

            
    $maxlim = array(=> 80,
                                   
    20 => 200,
                                   
    30 => 800,
                                   
    40 => 1800,
                                   
    50 => 3000,
                                   
    60 => 4500,
                                   
    70 => 5000,
                                   
    80 => 7000,
                                   
    90 => 9000,
                                   
    100 => 10000);

        if (
    $number 100) {
            echo 
    "Illegal level";
        }else{
                
    $number -= $number 10;
                
    $generate rand($minlim($number), $maxlim($number));
            }

    not tested, you must also check the values from $minlim and $maxlim

    best regards

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    oesxyl:

    I get an error when I enter in a number from the form:
    Fatal error: Function name must be a string in C:\xampp\htdocs\5\rpgpointgen.php on line 38

    Line 38 would be
    PHP Code:
    $generate rand($minlim($number), $maxlim($number)); 
    Any idea what the error means?

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Bob42 View Post
    oesxyl:

    I get an error when I enter in a number from the form:
    Fatal error: Function name must be a string in C:\xampp\htdocs\5\rpgpointgen.php on line 38

    Line 38 would be
    PHP Code:
    $generate rand($minlim($number), $maxlim($number)); 
    Any idea what the error means?
    $minlim and $maxlim are array, you must use
    '[' and ']' not '(' and ')'.

    PHP Code:
    $generate rand($minlim[$number], $maxlim[$number]); 
    Is my fault, I write '(', ')' in my code.

    best regards
    Last edited by oesxyl; 02-26-2008 at 01:04 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •