2

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.

1
  • 1
    I must say thank you for the 5 people who is helping me. I use Nacereddine solution because he's/her answer is straightforward. Thanks again to DBDev, Nacereddine, Adrian Brown, Michael and Ben. What a great community we have here :) Commented Oct 23, 2011 at 22:00

5 Answers 5

2

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']); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I take a look at 4 other answers and I must say your solution is very easy to understand and clean.
2

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 } 

1 Comment

You can copy/paste faster than I can.
1

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') ); } 

Comments

0

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

Comments

0
// 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 ################################## 

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.