Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Generating all the switch case

    Is there a way I can make a pull down menu generating a list off another file of all the destination case coming from the file locations.php?

    locations.php

    switch ($destination){
    case "Las Vegas":
    echo "Bring an extra $500";
    break;
    case "Amsterdam":
    echo "Bring an open mind";
    break;
    case "Egypt":
    echo "Bring 15 bottles of SPF 50 Sunscreen";
    break;
    case "Tokyo":
    echo "Bring lots of money";
    break;
    case "Caribbean Islands":
    echo "Bring a swimsuit";
    break;
    }
    Thanks

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    not really.
    A better way would be to have something like
    PHP Code:
    $destinations = array(
      
    'Las Vegas'=>'Bring an extra $500',
      
    'Amsterdam'=>'Bring an open mind'
      
    //etc.
    );

    //and then your code above can be replaced with:
    echo $destinations[$destination]; 

    //and the select can be made with:
    echo '<select name="destination">';
    foreach(
    $destinations as $destination=>$text) {
      echo 
    '<option value="'.$destination.'">'.$destination.'</option>';
    }
    echo 
    '</select>'
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. Is using the array better then switch case in general?

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,037
    Thanks
    2
    Thanked 316 Times in 308 Posts
    If you are just doing a 1 to 1 lookup/replacement, using an array would allow easy expansion, editing, or modification. If you need to do custom processing per entry, the switch/case statement would be the way to go.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •