Web Design and Web Development Forum

  1. #1
    Join Date
    Jan 2005
    Location
    Ohio
    Age
    23
    Posts
    202
    Rep Power
    8
  2. Vouksh is on a distinguished road
  3. Question Dynamic Switch?

    Well, I was wondering, is it possible to have a "dynamic" switch statement?
    kinda like...
    PHP Code:
    <?php
    $allowed 
    = array('codedb''members''forums''content');
    switch(
    strtolower($_GET['section']))
    {
        foreach(
    $allowed as $rd)
        {
            case 
    $rd:
                include 
    $rd.'.inc.php';
            break;
        }
    }
    ?>
    (this doesn't work btw)
    I guess there's always the possibility of writing it to the included file, but that' d be a pain in the butt, but it'd work, no?

    Gimme your thoughts on this.
    Reply With Quote Reply With Quote
  4. #2
    unclekyky's Avatar
    Join Date
    Sep 2004
    Age
    22
    Posts
    5,184
    Rep Power
    13
  5. unclekyky is on a distinguished road
  6. Re: Dynamic Switch?

    Just use an if statement inside the foreach.
    Spore-Game - The Ultimate Spore Fan-Site
    Abnegating Avunculicide Since 1601 | YC Wiki - "Quidquid latine dictum sit, altum sonatur."
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Oct 2005
    Posts
    18
    Rep Power
    0
  8. abdussamad is on a distinguished road
  9. Re: Dynamic Switch?

    Nice idea. Try this code:

    Code:
    <?php
    $allowed = array('codedb', 'members', 'forums', 'content');
    if(!empty($_GET['section']))
    {
       $rd=strtolower($_GET['section']);
    
       if(in_array($rd,$allowed)) 
                include $rd.'.inc.php';
    }
    ?>
    Or if you prefer using a switch statement:

    Code:
    <?php
    $allowed = array('codedb', 'members', 'forums', 'content');
    if(!empty($_GET['section']))
    {
        foreach($allowed as $rd)
        {
            switch(strtolower($_GET['section']))
             {  
                case $rd: include $rd.'.inc.php'; break;
             }  
        }
    }
    ?>
    Best Regards,
    Abdussamad
    http://abdussamad.com
    Reply With Quote Reply With Quote
  10. #4
    Join Date
    Dec 2005
    Posts
    2
    Rep Power
    0
  11. wGEric is on a distinguished road
  12. Re: Dynamic Switch?

    The first bit of code that bdussamad posted is the best way to do it. You shouldn't loop if you don't have to.
    Reply With Quote Reply With Quote
  13. #5
    Join Date
    Jun 2005
    Location
    California, USA
    Age
    22
    Posts
    2,821
    Rep Power
    0
  14. Mau is on a distinguished road
  15. Re: Dynamic Switch?

    Honestly, this code is very ineffecient:
    PHP Code:
    <?php
    $allowed 
    = array('codedb''members''forums''content');
    if(!empty(
    $_GET['section']))
    {
        foreach(
    $allowed as $rd)
        {
            switch(
    strtolower($_GET['section']))
             {  
                case 
    $rd: include $rd.'.inc.php'; break;
             }  
        }
    }
    ?>
    The same thing is accomplished much better by using the first bit of code posted above.
    Reply With Quote Reply With Quote
  16. #6
    IntellEJent's Avatar
    Join Date
    Jan 2005
    Location
    The restaurant at the end of the universe
    Age
    17
    Posts
    1,510
    Rep Power
    9
  17. IntellEJent is on a distinguished road
  18. Re: Dynamic Switch?

    In_array() is very inefficient, as Mau said. A switch statement is unreasonable if there's only one case, so therefore, I'd much prefer an if statement for this problem, or an altogether different solution altogether. May I also note that this code is vulnerable to register_globals injection? Anyways, here's what I came up with that's dissimilar to any of the others, but sadly is still vulnerable to register_globals injection:
    PHP Code:
    <?php
    $allowed 
    = array('codedb''members''forums''content');
    $iffed false;
    $get_section strtolower($_GET['section']);
    foreach(
    $allowed as $rd)
    {
        if(
    $get_section $rd)
        {
            
    $iffed $rd;
            include 
    $rd ".inc.php";
        }
    }
    if(
    $iffed === false)
    {
        die( 
    "You or your browser has inputted an invalid page!" );
    }
    ?>
    Reply With Quote Reply With Quote
  19. #7
    Join Date
    Oct 2005
    Posts
    18
    Rep Power
    0
  20. abdussamad is on a distinguished road
  21. Re: Dynamic Switch?

    Intellejent, Mau has in fact pointed out that the in_array() based code that I posted is the better solution. in_array() is a built in php function and is likely optimised by more experienced programmers than you or I (programmers that built the PHP language itself). Hence in_array() is the most efficient.
    Best Regards,
    Abdussamad
    http://abdussamad.com
    Reply With Quote Reply With Quote
  22. #8
    Join Date
    Jun 2005
    Location
    California, USA
    Age
    22
    Posts
    2,821
    Rep Power
    0
  23. Mau is on a distinguished road
  24. Re: Dynamic Switch?

    Additionally, Intellejent, your code just duplicates in_array()--except once it finds a match, it doesn't stop searching. Even if it did, in_array() would be a better choice because it's built in a lower programming level and should execute a tiny bit faster.
    Reply With Quote Reply With Quote

Similar Threads

  1. Dynamic Signature :: How do you?
    By tomcromp in forum PHP Scripting
    Replies: 5
    Last Post: 10-18-2005, 11:33 AM
  2. Cannot switch desktops (BCPPB 5.0)
    By MiraX33 in forum C and C++ Programming
    Replies: 3
    Last Post: 08-09-2005, 07:04 PM
  3. Static vs. Dynamic
    By dkrrunner in forum C and C++ Programming
    Replies: 1
    Last Post: 07-29-2005, 12:13 PM
  4. Making dynamic urls search engine friendly
    By Vio-Bear in forum Articles, Tutorials, and Guides
    Replies: 0
    Last Post: 06-09-2005, 01:14 PM
  5. Dynamic Clock
    By Aeris7282 in forum Javascript Articles
    Replies: 0
    Last Post: 01-30-2005, 09:45 AM