Switch case

Discussion in 'PHP' started by www.techdo.com, Apr 29, 2009.

  1. #1
    Is there a limit to the number of "cases" in the

    Switch ( ) {

    case..

    }

    ?

    i think i'm going to need 50/60 items


    thank you for your reply



    .
     
    www.techdo.com, Apr 29, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    No, there is no limit, but if you have 50 items, there's a good chance that there's some other better way to structure your logic.
     
    SmallPotatoes, Apr 29, 2009 IP
  3. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    As long as you have
    switch ($_GET["mode"]) {
    case "new":
    ?>
    PHP:
    		break;
    PHP:
    on your pages, you can have as many breaks; as you like
    and mode=new2 (example to enter the page)
    switch ($_GET["mode"]) {
    case "new2":
    ?>
    PHP:
     
    pixmania, Apr 29, 2009 IP
  4. darren884

    darren884 Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Make sure you add a closing bracket on that last statement }
     
    darren884, Apr 29, 2009 IP
  5. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi,

    Just thinking out loud here if its anything that needs that many switch statements it's going to be hard to test and debug... You could find alternate methods, arrays for example (page management) -

    
    $selPage = $_GET["page"];
    $pages = Array ("index"=>"home.php", "about"=>"about.php");
    
    foreach($pages as $key=>$val) { 
       if($key == $selPage) {
             //$val contains the page value i.e. home.php
             //Include the page, or header() redirect it.
       }
    }
    
    PHP:
    Then if you add another page, just add another element to the array - A lot lesser code and more cleaner than a page full of if/switch statements.

    PS. Code wrote from memory, looks ok but needs testing ;)

    Regards,

    Steve
     
    Steve136, Apr 29, 2009 IP
  6. www.techdo.com

    www.techdo.com Well-Known Member

    Messages:
    526
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #6
    interesting solution,

    but let's say i have a MySql table with 2 columns called "label" and "page" where i save the page name (label) and the page path (page) and i would create an array where the array's Key will have the "Label" column value and the array item's value will have the "Page" column value

    how could i obtain this ?

    thank you for your help




    .
     
    www.techdo.com, May 2, 2009 IP
  7. Steve136

    Steve136 Peon

    Messages:
    240
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It's not actually that much code, as we already have the setup for using an array to navigate the page - you just need to take the data from the database and put it in an array.

    
    <?php
    
    /* do all your mysql_connect()
     * stuff before anything else,
     * maybe even in an include file.
     */
    
    $selPage = $_GET["page"];
    $pages = Array();
    
    $sqlQry = mysql_query("SELECT `page`, `label` FROM `pages`");
    
    while($sqlResult = mysql_fetch_assoc($sqlQry)) {
    	$pages[]["label"] = $sqlResult["label"];
    	$pages[]["page"]  = $sqlResult["page"];
    }
    
    ?>
    
    PHP:
    The above code will fill an array $pages with the following -

    
    Array
    (
        [0] => Array
            (
                [label] => Label 1
                [page] => index.php
            )
        [1] => Array
            (
                [label] => Label 2
                [page] => index1.php
            )
        [2] => Array
            (
                [label] => Label 2
                [page] => index2.php
            )
        [3] => Array
            (
                [label] => Label 3
                [page] => index3.php
            )
    )
    
    Code (markup):
    If you wanted to use the above array with the code I posted previously, it would need a slight change:

    
    foreach($pages as $key) { 
       if($key["label"] == $selPage) {
             //Can either check the $key["label"] or $key["page"]
             //$val contains the page value i.e. home.php
             //Include the page, or header() redirect it.
       }
    }
    
    PHP:
    The above code now takes each entry of an array, as our new array is multidimensional we can use the array keys to do the checking.

    Regards,

    Steve
     
    Steve136, May 2, 2009 IP