Jump to content

question about switch() statement


boo_lolly

Recommended Posts

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]
<?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
                        */
        }
?>
[/code]

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...
Link to comment
https://forums.phpfreaks.com/topic/34480-question-about-switch-statement/
Share on other sites

I'm really not understanding your question, but switch statements have nothing to do with "reloading the page". They are simply a way in which to have mutiple possible outcomes for a given value. No different than if you used a bunch of elseif's
in which case I'd do this

[code]<?php

/*
                        *some if statements go here
                        *and set $displayType to
                        *1, 2, or 3... etc
*/

        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;
}
?>
[/code]

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.