/    Sign up×
Community /Pin to ProfileBookmark

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.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 08.2011 — First: you can help us to help you by making use of this forum's [noparse][code=php]...[/code][/noparse] 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.
Copy linkTweet thisAlerts:
@mich5blueauthorJun 08.2011 — 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.

[code=php]
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 == 0 ) 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'] == 0 && $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>";
}
}

[/code]


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.
×

Success!

Help @mich5blue spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...