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 3 of 3
  1. #1
    Regular Coder GO ILLINI's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    634
    Thanks
    0
    Thanked 7 Times in 7 Posts

    assigning default select tag

    I have a config file editor page that should read the config file, then automatically select a <option> from a <select> tag. I dont want to have to write an if-else statement for each line in the config file(there are over 200)
    and I came up with 2 ways to do this and was wondering which would be easier and less cpu intensive.
    1) Create a loop for each config option, and check on each step in the loop for what should be 'selected' by default.
    2) Make a function to check. EX:
    (checkifset prints out selected="selected")
    PHP Code:
    <option other stuff checkifset('config_option','value1'); >Value 1</option>
    <
    option other stuff checkifset('config_option','value2'); >Value 2</option>
    <
    option other stuff checkifset('config_option','value3'); >Value 3</option

    It seems that option 2 would be easier to code(since I already have the html coded out), but it may also be more cpu intensive.

    Any thoughts?

    Thanks,
    -Adam
    Why not thank me?

    http://adamsworld.name

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    I keep my config options in an array (makes it easier to add new options when you remember the bits you forgot or add new functionality.

    the below is just an example, I keep the actual option building stuff in a function so it can be reused but you get the general idea ?

    PHP Code:
    <?
    $options
    ['IMAGE_ENGINE']=array(
      
    'imagick_php'=>'Image Magick extension',
      
    'imagick'=>'Image Magick (Exec)',
      
    'gd'=>'GD PHP Extension'
    );
    ?>
    $curr here is the current configuration loaded into an array !
    <?
    foreach($options['IMAGE_ENGINE'] as $k=>$v){
        
    $sel=(isset($curr['IMAGE_ENGINE']) && $curr['IMAGE_ENGINE']==$k) ? 'selected="SELECTED" ' '' ;
        
    $IMAGE_ENGINE[]='<option '.$SEL.'value="'.$k.'">'.$v.'</option>';
    }
    #.........#
    echo implode("\n"$IMAGE_ENGINE);
    ?>
    Last edited by firepages; 07-21-2007 at 02:53 AM. Reason: 'checked' should be 'selected'
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Location
    Pilipinas
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try to use the function below...

    you can modify it as well..

    PHP Code:
    <?

    /* your SELECT options */
    $select_options = array        
        (
            
    'config option 1',
            
    'config option 2',
            
    'config option 3'
        
    );

    $selected_item 'config option 2'// your SELECTED option in SELECT



    echo createSelect($select_options,$selected_item);


    /* returns the html SELECT with the selected item
        @select_options = (array) select options
        @selected_item = the selected item in the options
    */
    function createSelect($select_options,$selected_item)
    {
        
    $select "<select name=\"cmb\">\n";
        
        foreach (
    $select_options as $option_value => $option_text )
        {
            
    $is_selected '';
            
            if(
    $option_text == $selected_item)
            {
                
    $is_selected "selected";
            }
            
            
    $select .= "<option value=\"".$option_value."\" ".$is_selected.">".$option_text."</option>";
        }
        
        
    $select .= "<\select>\n";
        
        return 
    $select;
    }


    ?>
    HTH


  •  

    Posting Permissions

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