www.webdeveloper.com
Results 1 to 3 of 3

Thread: combining arrays to build select dropdown

  1. #1
    Join Date
    May 2010
    Location
    Ann Arbor, MI
    Posts
    14

    combining arrays to build select dropdown

    Hello, I am wondering if someone can give some help on using two arrays to build a drop down, and save the values for each of them when needed without
    the constants colliding. Here is some information about the arrays used and the current foreach I use to build it.

    $share_types = array(
    SHARE_TYPE_AGENT => "Agent Software",
    SHARE_TYPE_FS => "File Share",
    SHARE_TYPE_EMAIL_EXCHANGE => "Message-Level Backup (Exchange)",
    SHARE_TYPE_EMAIL_GROUPWISE => "Message-Level Backup (GroupWise)",
    SHARE_TYPE_VMWARE => "VMware"
    );

    $share_protocols = array(
    SHARE_FS_PROTOCOL_CIFS => "File Share (CIFS)",
    SHARE_FS_PROTOCOL_SSHFS => "File Share (SSHFS)",
    SHARE_FS_PROTOCOL_NCPFS => "File Share (NCPFS)"
    );

    Here is the current foreach loop that will build the drop down. It is basically taking the share_type array and foreaching through it. (The few if
    statements in there are just hiding a few of the choices if certain circumstances are met)

    // block out anything except agent shares when asked
    if ( $lock_to_share_type_agent )
    {
    $html .= '<option value="'. SHARE_TYPE_AGENT .'" selected="selected">'. $share_types[SHARE_TYPE_AGENT] .'</option>';

    include_once(BASE_PATH .'/web/customer/lib/share/read.php');
    foreach( $share_types as $key => $value )
    {
    // don't show File Share type if network_fs_enabled is 0
    if ( $key == SHARE_TYPE_FS && $server_info['network_fs_enabled'] == 0 && $server_info['server_type'] == SERVER_TYPE_OSB_WINDOWS ) continue;

    // make sure account has access to agent services and that the server has agent support turned on
    if ( $key == SHARE_TYPE_AGENT && (!$agent_service_available || !$server_agent_enabled) ) continue;

    $selected = ( _fd("type", $field_data) == $key ) ? " selected=\"selected\"" : "";
    $html .= "<option value=\"{$key}\"{$selected}>{$value}</option>";
    }
    }

    Now what I want to do now is incorporate another array ($share_protocols) as seen above to display next to the file share if that is picked. I have tried doing
    something like seen below, but the constant values seem to be colliding. The problem is I need to save the constant value from the first array ($share_type),
    and also the constant value from the 2nd array if we are using it ($share_protocol) In a perfect world the drop down would be:

    File Share - CIFS
    File Share - SSHFS
    File Share - NCPFS
    then the rest...

    Saving both the share type (File Share) and the share protocol (CIFS, or SSHFS, or NCPFS) depending on what is selected.

    If anyone has any input I would greatly appreciate it. It is a bit hard to explain what I am trying to do so I apologize if it is confusing/overwheling.

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,326
    First: you can help us to help you by making use of this forum's [php]...[/php] bbcode tags around your code samples.

    Anyway, if I correctly followed all of that, it sounds like you just need an if/else within the foreach loop after the "continue;", where you either check if $key is SHARE_TYPE_FS or check if the $value is "file share", and if so put in a foreach loop on the second array at that point to output its options/values, then the rest of the code for the outer foreach loop would be in an ensuing else block.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    May 2010
    Location
    Ann Arbor, MI
    Posts
    14
    thanks NogDog. Sorry about the formatting. I will remember that for next time. So for displaying purposes, that if statement you suggested worked perfectly. There is one last thing that I need to be able to do though, and was wondering if you could provide any help.

    PHP Code:
            foreach( $share_types as $key => $value )
            {
                
    // only show types that make sense for current server selection
                
    if ( $server_info !== false && !in_array($key$share_types_by_server_type[$server_info["server_type"]]) ) continue;

                
    // don't show deprecated types for new shares
                
    if ( share_type_is_deprecated($key) && $share_id == ) continue;
                
                
    // don't show agent in dropdown when server already has an agent share
                
    if ($key == SHARE_TYPE_AGENT && isset($server_has_agent_share_list[$server_id]) && $server_has_agent_share_list[$server_id]['type'] == SHARE_TYPE_AGENT && $server_info['server_type'] == SERVER_TYPE_OSB_WINDOWS  && empty($field_data)) continue;

                
    // don't show File Share type if network_fs_enabled is 0
                
    if ( $key == SHARE_TYPE_FS && $server_info['network_fs_enabled'] == && $server_info['server_type'] == SERVER_TYPE_OSB_WINDOWS ) continue;

                
    // make sure account has access to email services
                
    if ( !$email_service_available && ($key == SHARE_TYPE_EMAIL_EXCHANGE || $key == SHARE_TYPE_EMAIL_GROUPWISE) ) continue;

                
    // make sure account has access to agent services and that the server has agent support turned on
                
    if ( $key == SHARE_TYPE_AGENT && (!$agent_service_available || !$server_agent_enabled) ) continue;
                
                if ( 
    $key == SHARE_TYPE_FS )
                {
                    foreach (
    $share_protocols as $protocol_key => $value_protocol)
                    {
                        if (
    $protocol_key == SHARE_FS_PROTOCOL_SSHFS && $server_info['server_type'] == SERVER_TYPE_OSB_WINDOWS) continue;
                        if (
    $protocol_key == SHARE_FS_PROTOCOL_NCPFS && $server_info['server_type'] == SERVER_TYPE_OSB_WINDOWS) continue;
                        if (
    $protocol_key == SHARE_FS_PROTOCOL_NCPFS && ($server_info['server_type'] == SERVER_TYPE_OSB_UNIX_SSHFS || $server_info['server_type'] == SERVER_TYPE_OSB_OSX)) continue;
                        if (
    $protocol_key == SHARE_FS_PROTOCOL_CIFS && ($server_info['server_type'] == SERVER_TYPE_OSB_UNIX_SSHFS || $server_info['server_type'] == SERVER_TYPE_OSB_OSX)) continue;
                        if (
    $protocol_key == SHARE_FS_PROTOCOL_SSHFS && $server_info['server_type'] == SERVER_TYPE_OSB_NETWARE) continue;
                        if (
    $protocol_key == SHARE_FS_PROTOCOL_NCPFS && $server_info['server_type'] == SERVER_TYPE_OSB_NETWARE && empty($field_data)) continue;
                        
                        
    $selected = ( _fd("type"$field_data) == $key ) ? " selected=\"selected\"" "";
                        
    $html .= "<option value=\"{$key}\"{$selected}>{$value} - {$value_protocol}</option>";
                    }
                if 
                }
                else
                {
                    
    $selected = ( _fd("type"$field_data) == $key ) ? " selected=\"selected\"" "";
                    
    $html .= "<option value=\"{$key}\"{$selected}>{$value}</option>";
                }
            } 
    This will display the correct contents without a problem. The only thing else that I need to be able to achieve is saving information to the database. So the select name is 'type' which is what the $key will save to, which does without a problem. However, I have another column in my database ('protocol_type') that I need to be able to save the value to as well if they have chosen SHARE_TYPE_FS. If they don't choose SHARE_TYPE_FS, I could save the value as 0 or null...but I just don't know where I could include this code to do so.

    I should add that in order to save they 'type' I can do so because it is named on the select (my save function will save it based on grabbing it because it is part of the field data)

    I also could be doing something completely wrong or out of the norm, so if you noticed anything feel free to tell me if I should be doing something another way or if it is a bad idea.

    Thanks so much for the help earlier.
    Last edited by mich5blue; 06-08-2011 at 03:43 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.16841 seconds
  • Memory Usage 2,913KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates