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 6 of 6

Thread: Select option

  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Select option

    I cant explain better, what i want(if server is 1 then show option "server name 1" or $server is ALL, then show all of server names):


    Code:
    $server == "from database"
    
    <select type="text" name="server">
    
    if ($server == "1")
    (
    <option value="1">Server name 1</option>
    )
    elseif ($server == "2")
    (
    <option value="2">Server name 2</option>
    )
    elseif ($server == "ALL")
    (
    <option value="ALL">Server name 1</option>
    <option value="ALL">Server name 2</option>
    )
    </select>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Which is right (branch wise), except you cannot apply a value of "ALL" to your option values. If they are not unique there is no way to determine what value was selected.

    I'm not sure what it is you are trying to do here. Select menus are pointless with only one option within them, and completely useless if multiple options have the same value to them. Given your $server variable as well, you'll end up with a select menu that has no options within it.
    So, uhh, what is it you are actually trying to do?

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,004
    Thanks
    15
    Thanked 236 Times in 236 Posts
    This code definitely needs to be smarter. First, put all your server names in an indexed array, that is by the number. I just write the code, OK.
    PHP Code:
    in the post part of your php put $svr $_POST['server_name'];
    make all the values in the options just numbersno need for the quotes, and add one more number than the number of servers for the 'all' optionin this case 2.
    remember that php numbers from 0
    $server_names
    =array("name1","name2");
    if ( 
    $svr != $name $server_names[$svr];
    else
    {
    // In here you put a for loop that prints out all the server names from $server_names


  • #4
    Regular Coder jdswebservice's Avatar
    Join Date
    Aug 2010
    Location
    Moon Township, PA
    Posts
    107
    Thanks
    0
    Thanked 5 Times in 5 Posts
    First off, your coding is HORRIBLE, unless this is pseudo code... :/ anyways heres a switch statement for ya.

    PHP Code:
    <?php
    //Define the variables here
    $server "some variable";
    $output "";
    //do your switch function!
    switch ($server){
        case 
    "1":
            
    $output "<option value=\"1\">Server 1</option>";
            break;
        case 
    "2":
            
    $output "<option value=\"2\">Server 2</option>";
            break;
        case 
    "ALL":
            
    $output "<option value=\"1\">Server 1</option>";
            
    $output .= "<option value=\"2\">Server 2</option";
            break;
        default:
            
    $output "<option value=\"1\">Server 1</option>";
            
    $output .= "<option value=\"2\">Server 2</option";
            break;
    }
    ?>
    <html>
    <head>
        <title> Coding Forums Post 267803</title>
    </head>
    <body>
        <form method="post" action="some_script.php">
            <select name="server_select">
                <?php echo($output); ?>
            </select>
            <br>
            <input type="submit" value="Submit this form">
        </form>
    </body>
    </html>
    Last edited by jdswebservice; 07-14-2012 at 08:50 PM. Reason: Forgot to put ($server) after the switch statement. lol
    Jonathan R.
    Email: jonathan@logicaldevelopment.net
    Skype, Aim, & Jabber: logicaldevelopment
    Website: http://logicaldevelopment.net/

    NOTE: When contacting me please have the following things ready:

    Thread ID (This can be found in the URL of your thread)
    Budget of Project
    Methods of contacting you/your organization
    Preferred method if many methods are provided

    Any website information that you PM, Email, IM, or otherwise is strictly confidential and will not be disseminated, distributed, or copied in any way, shape or form.

  • #5
    New Coder
    Join Date
    May 2012
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!

    Code:
    <?php
    
    $db_value = "ALL";
    
    switch($db_value)
    {
         case '1':
              $output = "<option value=\"1\">Server 1</option>";
              break;
    
         case '2':
              $output = "<option value=\"2\">Server 2</option>";
              break;
    		  
         case 'ALL':
              $output = "<option value=\"1\">Server 1</option>";
    		  $output .= "<option value=\"2\">Server 2</option";
              break;
    }
    ?>
    
    <select name="server">
    <?php echo($output); ?>
    </select>

  • #6
    Regular Coder jdswebservice's Avatar
    Join Date
    Aug 2010
    Location
    Moon Township, PA
    Posts
    107
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Hopefully that helps and does what you wanted!

    #resolved!
    Jonathan R.
    Email: jonathan@logicaldevelopment.net
    Skype, Aim, & Jabber: logicaldevelopment
    Website: http://logicaldevelopment.net/

    NOTE: When contacting me please have the following things ready:

    Thread ID (This can be found in the URL of your thread)
    Budget of Project
    Methods of contacting you/your organization
    Preferred method if many methods are provided

    Any website information that you PM, Email, IM, or otherwise is strictly confidential and will not be disseminated, distributed, or copied in any way, shape or form.


  •  

    Posting Permissions

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