I want delete user and user role by using checkbox. first check then submit button click. After click, selected user and user_role should be deleted.
got php undefined offset 2 error on line 491
this is my model:
public function add_participation(){
$user = $this->input->post('user');
$role = $this->input->post('role');
$delete = $this->input->post('delete');
for($i=0;$i<count($user);$i++){
if($user[$i] !=""){
$this->db->where('workflow_activity_id',$this->input->post('batch'));
$this->db->where('role_id',$role[$i]);
$this->db->where('user_id',$user[$i]);
$exist = $this->db->get('workflow_participation');
$data = array(
'user_id' => $user[$i],
'role_id' => $role[$i],
'workflow_activity_id' => $this->input->post('batch'),
);
if($exist->num_rows() == 0){
$this->db->insert('workflow_participation',$data);
}else{
$this->db->where('workflow_activity_id',$this->input->post('batch'));
$this->db->where('role_id',$role[$i]);
$this->db->where('user_id',$user[$i]);
$this->db->update('workflow_participation',$data);
}
if($delete[$i] == '1'){ //**error on this line**
$this->db->where('workflow_activity_id',$this->input->post('batch'));
$this->db->where('role_id',$role[$i]);
$this->db->where('user_id',$user[$i]);
$this->db->delete('workflow_participation');
}
}
}
return true;
}
In this view page user and user_role show in drop-down.
This is my view page
<div class="form-group">
<label class="control-label col-md-3">User :</label>
<div class="col-md-8">
<select id="user" name="user[]" class="select form-control">
<option value="" selected="selected">-------</option>
<?php
if(!empty($user)){
foreach($user as $user_result){?>
<option value="<?=$user_result->id;?>" <?php if(!empty($participent) && !empty($participent[1]) && $participent[1]->user_id == $user_result->id){?>selected="selected"<?php }?>><?=$user_result->username;?></option>
<?php }}?>
</select>
</div>
<label class="control-label col-md-3">Role :</label>
<div class="col-md-8">
<select id="role" name="role[]" class="select form-control">
<option value="" selected="selected">-------</option>
<?php
if(!empty($role)){
foreach($role as $role_result){?>
<option value="<?=$role_result->id;?>" <?php if(!empty($participent) && !empty($participent[1]) && $participent[1]->role_id == $role_result->id){?>selected="selected"<?php }?>><?=$role_result->name;?></option>
<?php }}?>
</select>
</div>
<label class="control-label col-md-3">Delete :</label>
<div class="col-md-8">
<input type="checkbox" name="delete[]" value="1">
</div>
First check if the variable $role have the same amount of data as $user, if it's not so you have a problem.
Next for the line for($i=0;$i<count($user);$i++)
, create an outside variable for your count, here everytime you enter the for, the count() is called, bad optimisation.
Since you are using delete[] as array, i hope your view is in a loop. a checkbox value is submitted only when it is checked. So you will receive your input in wrong order. ie, you selected first user and role and checked last delete, you may receive delete for first user. The second and third delete will be undefined. Please see this link for details and hidden field hack for this issue
So I suggest change the following 3 lines in your html as following. you can use $participent[1]->user_id ( or a counter) as index in view file.
<select id="user" name="user[<?php echo $participent[1]->user_id;?>]" class="select form-control">
...
<select id="role" name="role[<?php echo $participent[1]->user_id;?>]" class="select form-control">
...
<input type="checkbox" name="delete[<?php echo $participent[1]->user_id;?>]" value="1">
change the loop from this
for($i=0;$i<count($user);$i++){
to
foreach( $user as $i => $unused ) {
Then instead of equal checking
if($delete[$i] == '1'){ //**error on this line**
use
if( isset($delete[$i]) ){ //**error on this line**
// if( $delete[$i] == '1' ){ //uncomment if you need double confirmation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.