I am creating a web application using codeigniter 3. I have created the database and views and models for user and for admin also now i stucked at some point where i need to give permission to admin so that he can delete a user information or can update it. How to do this.
Here is my edit controller view and model code respectively
//#### User Edit Controller
load->model(‘User_model’); // Correct model loading
$this->load->helper(‘url’); // Load the URL helper
$this->load->library(‘form_validation’); // Load form validation library
}
// Display the edit form with user details
public function edit_user($userid) {
$data[‘user’] = $this->User_model->get_user_by_userid($userid); // Get user data based on userid
if (!$data[‘user’]) {
show_404(); // Show 404 if user not found
}
$this->load->view(‘update_user’, $data); // Load view with user data
}
// Update user details in the database
public function update_user($userid) {
// Set form validation rules
$this->form_validation->set_rules(‘userid’, ‘User ID’, ‘required’);
$this->form_validation->set_rules(‘role’, ‘Role’, ‘required’);
$this->form_validation->set_rules(‘password’, ‘Password’, ‘required’);
if ($this->form_validation->run() == FALSE) {
// Validation failed, reload the edit form with existing data
$this->edit_user($userid);
} else {
// Prepare user data for update
$user_data = array(
‘userid’ => $this->input->post(‘userid’),
‘role’ => $this->input->post(‘role’),
‘password’ => $this->input->post(‘password’)
);
// Update user in the database
$this->User_model->update_user_by_userid($userid, $user_data);
// Redirect back to the edit page for further updates
redirect(‘UserEditController/edit_user/’ . $userid);
}
}
}
//#### User Edit View
Edit User Information
userid); ?>” method=”POST”>
<!-- User ID (Disabled, as it shouldn't be editable) -->
<div class="form-group">
<label for="userid">User ID</label>
<input type="text" class="form-control" id="userid" name="userid" value="<?php echo set_value('userid', $user->userid); ?>" readonly>
</div>
<!-- Role -->
<div class="form-group">
<label for="role">Role</label>
<select class="form-control" id="role" name="role" required>
<option value="">Select Role</option>
<option value="admin" <?php echo set_select('role', 'admin', ($user->role == 'admin')); ?>>ADMIN</option>
<option value="user" <?php echo set_select('role', 'user', ($user->role == 'user')); ?>>USER</option>
</select>
<?php echo form_error('role'); ?>
</div>
<!-- Password -->
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
<?php echo form_error('password'); ?>
</div>
<!-- Submit Button -->
<div class="form-group">
<button type="submit" class="btn btn-success">
<i class="fas fa-save"></i> Update User
</button>
</div>
</form>
//#### User Edit Model
db->insert(‘users’, $data);
$data = $this->input->post();
}
public function get_user_by_userid($userid) {
$this->db->where(‘userid’, $userid);
$query = $this->db->get(‘users’); // Replace ‘users’ with your table name
return $query->row(); // Return a single row
}
// Update user details
public function update_user_by_userid($userid, $user_data) {
$this->db->where(‘userid’, $userid);
return $this->db->update(‘users’, $user_data); // Replace ‘users’ with your table name
}
}
?>
Ib have placed it in the user model