2017-09-28 20 views
0

チェックボックスを使用してユーザーとユーザーロールを削除します。最初に確認してボタンをクリックしてください。クリック後、選択したユーザーとuser_roleを削除する必要があります。エラー未定義オフセット:phpコードイグナイターで2

PHPが未定義しまったが、ライン上で491

を2オフセット誤差これは私のモデルである:このビューのページのユーザーとドロップダウンでUSER_ROLEショーで

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

。それはあなたが問題を抱えているそうでなければ、変数$の役割は、$ユーザとして同じ量のデータを持っている場合

これが私の見解ページ

<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> 

+0

対応するHTMLを投稿してください。 – tan

+0

dd($ delete)を追加して を追加してください$ をチェックしてみてください空き($ delete) – BRjava

+0

@tan上の対応するページを追加しました –

答えて

0

あなたは配列としてdelete []を使用しているので、私はあなたのビューがループ内にあることを望みます。 チェックボックスの値は、チェックされている場合にのみ送信されます。あなたは間違った順序であなたの入力を受け取ります。 つまり、最初のユーザーと役割を選択して最後の削除をチェックした場合、最初のユーザーの削除が表示されることがあります。 2番目と3番目の削除は定義されません。 Please see this link for details and hidden field hack for this issue

このように、次の3行をhtmlで次のように変更することをお勧めします。 $ participant [1] - > user_id(またはカウンタ)をビューファイルのインデックスとして使用できます。

<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"> 

変化確認等しいの代わりに続い

foreach($user as $i => $unused) { 

この

for($i=0;$i<count($user);$i++){ 

からループ

if($delete[$i] == '1'){ //**error on this line** 

使用

if(isset($delete[$i])){ //**error on this line** 
    // if($delete[$i] == '1'){ //uncomment if you need double confirmation 
0

最初のチェックです。

次に、for($i=0;$i<count($user);$i++)行の外部変数を作成します。ここではforを入力するたびにcount()が呼び出され、不正な最適化が行われます。

関連する問題