Page 1 of 1

need some switch statement info

Posted: Tue Jan 16, 2007 4:35 pm
by boo_lolly
i have a switch statement that each case will display the products of an inventory in a different manner. as you can see the default; in the switch statement is where the variable will go through a few if statements and dictate what case; it should be (how the products should be displayed). i'm not sure if this will work the way i want it to, as in, i'm not sure switch statements work this way. but after a few if statements, the code will come to a conclusion and decide what the case should be, and it sets that variable to the correct value. i want the switch statement to emmediately change it to that case. does the page need to be reloaded in order for it to work correctly? does the default need to be written BEFORE the rest of the cases? maybe take out the 'break' command? here's my code...

Code: Select all

<?php
        switch($displayType){
                case 1;
                        /*display items in this way*/
                break;

                case 2;
                        /*display items in a different way*/
                break;

                case 3;
                        /*display items in another way*/
                break;

                default;
                       /*
                        *some if statements go here
                        *and set $displayType to 
                        *1, 2, or 3... etc
                        */
        }
?>
i'm not getting any errors or anything.. that's just there to help you understand my question. if switch() statements can do that without reloading the page...

Posted: Tue Jan 16, 2007 4:40 pm
by feyd
That's not how switch statements work.

Place the code found in the default before the switch starts so it runs before it.

Posted: Tue Jan 16, 2007 4:46 pm
by boo_lolly
feyd wrote:That's not how switch statements work.

Place the code found in the default before the switch starts so it runs before it.
good idea. so the code written to decide which displayType to use is before the switch statement and completely outside of the switch statement all together... i can do that =). thanks feyd!

Posted: Tue Jan 16, 2007 4:57 pm
by Luke
notice the replacement of semi-colons with regular colons:

Code: Select all

switch($displayType)
{
    case 1:
    case 2:
        // this executes if $displayType == 1 or $displayType == 2
        break

    case 3:
        // this executes if $displayType == 3
        break;
}

Posted: Tue Jan 16, 2007 5:28 pm
by boo_lolly
thanks ninja. i needed that