2016-01-06 94 views
1

我想根據用戶登錄禁用我的下拉菜單。 我在會話中存儲用戶類型。如何根據用戶登錄啓用禁用下拉菜單?

現在管理員創建用戶。然後該用戶可以登錄並編輯他的詳細信息。

我想只允許某些字段被該用戶編輯。

就像我不想讓他編輯他的user_type。

現在,如果我從我的表單字段中刪除它們會出現數據在mysql中截斷的錯誤。如果我通過html禁用它,它顯示相同的錯誤。

請幫我

的是控制器代碼:

public function edit ($id = NULL) 
{ 
    $user_type = $this->session->userdata('user_type'); 


    if($user_type =="admin") 
     { 

      if ($id) 
       { 
        $this->data['user'] = $this->user_m->get($id); 
        count($this->data['user']) || $this->data['errors'][] = 'User could not be found'; 



        // Set up the form 
        $rules = $this->user_m->rules_admin; 
        $id || $rules['password']['rules'] .= '|required'; 
        $this->form_validation->set_rules($rules); 

           // Process the form 
        if ($this->form_validation->run() == TRUE) 
        { 

         $data = $this->user_m->array_from_post(array('emp_id','name','last_name','email','password','phone','gender','designation','user_type','blood_group','date_birth','status','address')); 

         if(!empty($data['password'])) 
         { 
          $data['password'] = $this->user_m->hash($data['password']); 
         } else { 
          // We don't save an empty password 
          unset($data['password']); 
         } 
         $key=$this->user_m->save($data, $id); 

         redirect('admin/user/index'); 
        } 

       } 

         // Load the view 
         $this->data['subview'] = 'admin/users/edit'; 
         $this->load->view('admin/_layout_main', $this->data); 

     } 

    elseif($user_type=="employee") 
     { 



      if ($id) 
      { 
       $this->data['user'] = $this->user_m->get_emp($id); 

       count($this->data['user']) || $this->data['errors'][] = 'User could not be found'; 

       $rules = $this->user_m->rules_admin; 
       $id || $rules['password']['rules'] .= '|required'; 
       $this->form_validation->set_rules($rules); 

         // Process the form 
       if ($this->form_validation->run() == TRUE) 
       { 

        $data = $this->user_m->array_from_post(array('emp_id','name','last_name','email','password','phone','gender','designation','user_type','blood_group','date_birth','status','address')); 


        //$data['password'] = $this->user_m->hash($data['password']); 

        if(!empty($data['password'])) 
        { 
         $data['password'] = $this->user_m->hash($data['password']); 
        } else { 
         // We don't save an empty password 
         unset($data['password']); 
        } 

        $id = $this->session->userdata('id'); 


        $this->user_m->save($data, $id); 


        redirect('admin/user/index'); 
       } 

      } 

      // Load the view 
      $this->data['subview'] = 'employee/profile/edit'; 
      $this->load->view('employee/_layout_main', $this->data); 



     } 

    else 
    { 
     echo "You Seem To Be Lost"; 
    } 


} 

編輯視圖

 <h3><?php echo empty($user->id) ? '<i class="glyphicon glyphicon-user"></i> Add a User' : 'Edit User ' . $user->name; ?></h3> 
    <?php echo validation_errors(); ?> 
    <?php echo form_open(); ?> 
    <table class="table"> 

      <tr> 

    <td>Employoee ID</td> 
    <td><?php echo form_input('emp_id', set_value('emp_id', $user->emp_id)); ?></td> 

</tr> 



<tr> 

    <td>Name</td> 
    <td><?php echo form_input('name', set_value('name', $user->name)); ?></td> 

</tr> 

<tr> 

    <td>Last Name</td> 
    <td><?php echo form_input('last_name', set_value('last_name', $user->last_name)); ?></td> 

</tr> 



<tr> 

    <td>Email</td> 
    <td><?php echo form_input('email', set_value('email', $user->email)); ?></td> 

</tr> 


<tr> 

    <td>Password</td> 
    <td><?php echo form_password('password'); ?></td> 

</tr> 



<tr> 

    <td>Phone</td> 
    <td><?php echo form_input('phone', set_value('phone', $user->phone)); ?></td> 

</tr> 

<tr> 

    <td>Gender</td> 
    <td><?php echo form_dropdown('gender', array('Male' => 'Male', 'Female' => 'Female'), $this->input->post('gender') ? $this->input->post('gender') : $user->gender); ?></td>  

</tr> 


<tr> 

    <td>Designation</td> 
    <td><?php echo form_input('designation', set_value('designation', $user->designation)); ?></td> 

</tr> 

<tr> 

    <td>User Type</td> 
    <td><?php echo form_dropdown('user_type', array('admin' => 'admin', 'employee' => 'employee','support_staff'=>'support_staff'), $this->input->post('user_type') ? $this->input->post('user_type') : $user->user_type); ?></td> 

</tr> 


<tr> 

    <td>Blood Group</td> 
    <td><?php echo form_input('blood_group', set_value('blood_group', $user->blood_group)); ?></td> 

</tr> 

<tr> 

    <td>Date Of Birth</td> 
    <td><?php echo form_input('date_birth', set_value('date_birth', $user->date_birth)); ?></td> 

</tr> 

<tr> 
      <td>Status</td> 
      <td><?php echo form_dropdown('status', array('Active' => 'Active', 'Inactive' => 'inactive', 'Delete' => 'delete'), $this->input->post('status') ? $this->input->post('status') : $user->status); ?></td> 
</tr> 



<tr> 

    <td>Address</td> 
    <td><?php echo form_textarea('address', set_value('address', $user->date_birth)); ?></td> 

</tr> 



<tr> 
    <td></td> 
    <td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"','onClick="image()"'); ?></td> 
</tr> 




    </table> 




    <script> 
    $(function(){ 
    $('#spinnerInput').spinner(); 
    }); 
    </script> 




    <?php echo form_close();?> 
+0

我建議適當縮進/格式代碼,如果你希望有人幫你 – DonCallisto

+0

@DonCallisto是的,我會編輯 – Rajan

回答

0

您可以登錄的用戶類型申請檢查。

If (admin) { 
    // SHOW DROP DOWN 
} 
else { 
    // SHOW JUST TEXT (SELECTED ONE FROM DROP DOWN) 
    // HIDE THE DROP DOWN BOX USING CSS PROPERTY: `display:none` 
} 
+0

但是,當我提交表單,我將在這裏得到錯誤 – Rajan

+0

認沽錯誤。 – Pupil

+0

針對user_type截斷的數據 – Rajan

1
If ($user_type =="admin") { 
    $select = ''; 
} 
elseif($user_type=="employee"){ 
    $select = 'disabled'; 
} 
// to prevent warning use this<?php isset($select) ? $select : ''; ?> 
// Above is shortcode for if(isset($select)){echo $select;}else{echo '';} 

<select <?php isset($select) ? $select : ''; ?> > 
    <option value="volvo">Volvo</option> 
    <option value="saab">Saab</option> 
    <option value="mercedes">Mercedes</option> 
    <option value="audi">Audi</option> 
</select> 
相關問題