PHP 中的 if else 与数组
我的代码需要帮助。 if 语句将检查会话的用户权限。如果它是管理员,它将显示 active()
数组,如果不是,则不会显示 active()
。无论如何我可以优化这段代码吗?我不想只是为了停用 active()
数组而编写两次相同的代码?
if($_SESSION["s"]["user"]["typ"] == 'admin') {
$form["tabs"]['dns_soa'] = array (
'title' => "DNS Zone",
'width' => 100,
'template' => "templates/dns_soa_edit.htm",
'fields' => array (
##################################
# Begin Datatable fields
##################################
'update_acl' => array (
'datatype' => 'VARCHAR',
'formtype' => 'TEXT',
'default' => '',
'value' => '',
'width' => '30',
'maxlength' => '255'
),
'active' => array (
'datatype' => 'VARCHAR',
'formtype' => 'CHECKBOX',
'default' => 'Y',
'value' => array(0 => 'N',1 => 'Y')
),
##################################
# ENDE Datatable fields
##################################
)
);
}
else {
$form["tabs"]['dns_soa'] = array (
'title' => "DNS Zone",
'width' => 100,
'template' => "templates/dns_soa_edit.htm",
'fields' => array (
##################################
# Begin Datatable fields
##################################
'update_acl' => array (
'datatype' => 'VARCHAR',
'formtype' => 'TEXT',
'default' => '',
'value' => '',
'width' => '30',
'maxlength' => '255'
),
##################################
# ENDE Datatable fields
##################################
)
);
}
提前致谢。
I need help with my code. The if statement will check session for user privilege. If it's admin it will show the active()
array and if not the active()
will not be shown. Is there anyway I could optimize this code? I don't want to coded the same code twice just to deactivate the active()
array?
if($_SESSION["s"]["user"]["typ"] == 'admin') {
$form["tabs"]['dns_soa'] = array (
'title' => "DNS Zone",
'width' => 100,
'template' => "templates/dns_soa_edit.htm",
'fields' => array (
##################################
# Begin Datatable fields
##################################
'update_acl' => array (
'datatype' => 'VARCHAR',
'formtype' => 'TEXT',
'default' => '',
'value' => '',
'width' => '30',
'maxlength' => '255'
),
'active' => array (
'datatype' => 'VARCHAR',
'formtype' => 'CHECKBOX',
'default' => 'Y',
'value' => array(0 => 'N',1 => 'Y')
),
##################################
# ENDE Datatable fields
##################################
)
);
}
else {
$form["tabs"]['dns_soa'] = array (
'title' => "DNS Zone",
'width' => 100,
'template' => "templates/dns_soa_edit.htm",
'fields' => array (
##################################
# Begin Datatable fields
##################################
'update_acl' => array (
'datatype' => 'VARCHAR',
'formtype' => 'TEXT',
'default' => '',
'value' => '',
'width' => '30',
'maxlength' => '255'
),
##################################
# ENDE Datatable fields
##################################
)
);
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这应该很容易
This should be easy
这应该可以做到:
稍后,为了显示,您可以使用 isset 检查 active 是否存在
This should do it:
And later on, to display, you can check for the existence of active with isset
首先初始化没有
$active
数组的数组,如果用户是管理员,则添加它:Initialize the array without the
$active
array first, then add it if the user is an admin:类似的东西
还没有尝试过,但你明白了
Soemthing like
Havent tried it, but you get the idea
}
}