Page 1 of 1

Switch eregi proglem

Posted: Mon Aug 03, 2009 7:16 am
by papa
Hi,

Code: Select all

 
                switch($unit['min_size']) {
                case 0:
                    $unit_size = "<i>You may only include one in your army</i>";
                    break;
                case 1:
                    $unit_size = "";
                    break;
                case eregi('^[0-9]+-[0-9]+$', $unit['min_size']):
                    $unit_size = $unit['min_size'];
                    break;
                default:
                    $unit_size = "Unit Size: {$unit['min_size']}+";
                }
 
Trying to include an eregi statement in my switch, but doesn't seem to work very well.

I have a unit size column which has for example: 0, 1, 10, 20-30

I want the eregi to recognize the 20-30 and change the $unit_size variable accordingly. Any way I can get this to work within the switch or should I just add an if statement with the eregi before the switch ?

Re: Switch eregi proglem

Posted: Mon Aug 03, 2009 7:47 am
by jazz090
you are using switch in an unorthodox fashion in which you are mixing integers and booleans together. Strictly speaking a variable cannot be an integer and a boolean so therefore i suggest you put the eregi (personally i would use preg_match) in an if statement and change the $unit['main_size'] to equal 2 should the if statement be executed. then simply call case 2:
of course you need to back it up before you change it because as i can see in your code, you are re-using it again.

this may solve your problem.

Code: Select all

if (eregi('^[0-9]+-[0-9]+$', $unit['min_size'])){
    $backup = $unit['min_size'];
    $unit['min_size'] = 2;
}
switch($unit['min_size']) {
    case 0:
        $unit_size = "<i>You may only include one in your army</i>";
        break;
    case 1:
        $unit_size = "";
        break;
    case 2:
        $unit_size = $backup;
        break;
    default:
        $unit_size = "Unit Size: {$unit['min_size']}+";
}

Re: Switch eregi proglem

Posted: Mon Aug 03, 2009 7:55 am
by papa
Thanks,

"unorthodox fashion" is that a nice way of saying it's poor coding ? :)

This is what I did:

Code: Select all

 
if(eregi('^[0-9]+-[0-9]+$', $unit['min_size'])) $unit_size = "Unit Size: {$unit['min_size']}";
else {
                    switch($unit['min_size']) {
                    case 0:
                        $unit_size = "<i>You may only include one in your army</i>";
                        break;
                    case 1:
                        $unit_size = "";
                        break;
                    default:
                        $unit_size = "Unit Size: {$unit['min_size']}+";
                    }
                }

Re: Switch eregi proglem

Posted: Mon Aug 03, 2009 7:55 am
by kelv1n
Try to avoid using eregi, it has been deprecated in 5.3, use preg_match() with the 'i' modifier instead (see http://us.php.net/manual/en/migration53.deprecated.php)

Re: Switch eregi proglem

Posted: Mon Aug 03, 2009 8:36 am
by jackpf
I think a problem could be data types.

If min_size can be 20-30 like you said, then that's a string...not an integer.

Comparing a string to an integer value of 0 will always return true.

Take this example:

Code: Select all

 
<?php
    $var = 'this is a string....see? :)';
    
    switch($var)
    {
        case 0:
            echo 'it was 0';
        break;
        case 1:
            echo 'it was 1';
        break;
    }
?>
 
Now, even though $var is obviously not 0, it says it is. However, this should work:

Code: Select all

<?php
    $var = 'this is a string....see? :)';
    
    switch($var)
    {
        case (string) 0: //or '0'
            echo 'it was 0';
        break;
        case (string) 1: //or '1'
            echo 'it was 1';
        break;
        default:
            echo 'it was a string';
        break;
    }
?>

:)
Sorry if this is completely not of use whatsoever...

Re: Switch eregi proglem

Posted: Mon Aug 03, 2009 8:44 am
by papa
No it was very useful thank you, just noticed that the script didn't work 100%, so have to take another look.

edit: It actually did work as expected. :)

Thank you guys.