PHP 中的 if else 与数组

发布于 2024-12-11 18:30:04 字数 1819 浏览 0 评论 0原文

我的代码需要帮助。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故乡的云 2024-12-18 18:30:04

这应该很容易

$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
    ##################################
    )
);

if($_SESSION["s"]["user"]["typ"] != 'admin') {
    unset($form["tabs"]['dns_soa']['fields']['active']);
}

This should be easy

$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
    ##################################
    )
);

if($_SESSION["s"]["user"]["typ"] != 'admin') {
    unset($form["tabs"]['dns_soa']['fields']['active']);
}
天涯离梦残月幽梦 2024-12-18 18:30:04

这应该可以做到:

$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
    ##################################
    )
);

if($_SESSION["s"]["user"]["typ"] == 'admin') {
    $form["tabs"]['dns_soa']['active'] =array (
            'datatype'  => 'VARCHAR',
            'formtype'  => 'CHECKBOX',
            'default'   => 'Y',
            'value'     => array(0 => 'N',1 => 'Y')
        );
}

稍后,为了显示,您可以使用 isset 检查 active 是否存在

if (isset($form["tabs"]['dns_soa']['active']))
{
    // do something with it
}

This should do it:

$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
    ##################################
    )
);

if($_SESSION["s"]["user"]["typ"] == 'admin') {
    $form["tabs"]['dns_soa']['active'] =array (
            'datatype'  => 'VARCHAR',
            'formtype'  => 'CHECKBOX',
            'default'   => 'Y',
            'value'     => array(0 => 'N',1 => 'Y')
        );
}

And later on, to display, you can check for the existence of active with isset

if (isset($form["tabs"]['dns_soa']['active']))
{
    // do something with it
}
自在安然 2024-12-18 18:30:04

首先初始化没有 $active 数组的数组,如果用户是管理员,则添加它:

$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
    ##################################
    )
);

if($_SESSION["s"]["user"]["typ"] == 'admin') {
  $form["tabs"]["dns_soa"]["active"] = array (
            'datatype'  => 'VARCHAR',
            'formtype'  => 'CHECKBOX',
            'default'   => 'Y',
            'value'     => array(0 => 'N',1 => 'Y')
        );
}

Initialize the array without the $active array first, then add it if the user is an 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'
        )
    ##################################
    # ENDE Datatable fields
    ##################################
    )
);

if($_SESSION["s"]["user"]["typ"] == 'admin') {
  $form["tabs"]["dns_soa"]["active"] = array (
            'datatype'  => 'VARCHAR',
            'formtype'  => 'CHECKBOX',
            'default'   => 'Y',
            'value'     => array(0 => 'N',1 => 'Y')
        );
}
稚然 2024-12-18 18:30:04

类似的东西

$form["tabs"]['dns_soa'] = array ( 
'title'     => "DNS Zone", 
'width'     => 100, 
'template'  => "templates/dns_soa_edit.htm", 
'fields'    => array ( 
    'update_acl' => array ( 
        'datatype'  => 'VARCHAR', 
        'formtype'  => 'TEXT', 
        'default'   => '', 
        'value'     => '', 
        'width'     => '30', 
        'maxlength' => '255' 
    ),
    )
);

if($_SESSION["s"]["user"]["typ"] == 'admin') { 
     $form["tabs"]['dns_soa'][fields]['active'] = array (      
        'datatype'  => 'VARCHAR',      
        'formtype'  => 'CHECKBOX',      
        'default'   => 'Y',      
        'value'     => array(0 => 'N',1 => 'Y')      
    );
}

还没有尝试过,但你明白了

Soemthing like

$form["tabs"]['dns_soa'] = array ( 
'title'     => "DNS Zone", 
'width'     => 100, 
'template'  => "templates/dns_soa_edit.htm", 
'fields'    => array ( 
    'update_acl' => array ( 
        'datatype'  => 'VARCHAR', 
        'formtype'  => 'TEXT', 
        'default'   => '', 
        'value'     => '', 
        'width'     => '30', 
        'maxlength' => '255' 
    ),
    )
);

if($_SESSION["s"]["user"]["typ"] == 'admin') { 
     $form["tabs"]['dns_soa'][fields]['active'] = array (      
        'datatype'  => 'VARCHAR',      
        'formtype'  => 'CHECKBOX',      
        'default'   => 'Y',      
        'value'     => array(0 => 'N',1 => 'Y')      
    );
}

Havent tried it, but you get the idea

生生漫 2024-12-18 18:30:04
// Presume user is NOT admin and populate $form
$form["tabs"]['dns_soa'] = array (
    'title'     => "DNS Zone",
    'width'     => 100,
    'template'  => "templates/dns_soa_edit.htm",
    'fields'    => array (
        'update_acl' => array (
            'datatype'  => 'VARCHAR',
            'formtype'  => 'TEXT',
            'default'   => '',
            'value'     => '',
            'width'     => '30',
            'maxlength' => '255'
        )
    )
);
##################################
# ENDE Datatable fields
##################################  

if($_SESSION["s"]["user"]["typ"] == 'admin') {

$form["tabs"]['dns_soa']['fields']['active'] = array (
##################################
# Begin Datatable fields
##################################
        'datatype'  => 'VARCHAR',
        'formtype'  => 'CHECKBOX',
        'default'   => 'Y',
        'value'     => array(0 => 'N',1 => 'Y')
    );
##################################
# ENDE Datatable fields
##################################

}

// Presume user is NOT admin and populate $form
$form["tabs"]['dns_soa'] = array (
    'title'     => "DNS Zone",
    'width'     => 100,
    'template'  => "templates/dns_soa_edit.htm",
    'fields'    => array (
        'update_acl' => array (
            'datatype'  => 'VARCHAR',
            'formtype'  => 'TEXT',
            'default'   => '',
            'value'     => '',
            'width'     => '30',
            'maxlength' => '255'
        )
    )
);
##################################
# ENDE Datatable fields
##################################  

if($_SESSION["s"]["user"]["typ"] == 'admin') {

$form["tabs"]['dns_soa']['fields']['active'] = array (
##################################
# Begin Datatable fields
##################################
        'datatype'  => 'VARCHAR',
        'formtype'  => 'CHECKBOX',
        'default'   => 'Y',
        'value'     => array(0 => 'N',1 => 'Y')
    );
##################################
# ENDE Datatable fields
##################################

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文