【问题标题】:Error undefined offset: 2 in php code-igniter错误未定义的偏移量:php 代码点火器中的 2
【发布时间】:2017-09-28 08:25:35
【问题描述】:

我想使用复选框删除用户和用户角色。首先检查然后提交按钮单击。点击后,选中的用户和user_role应该会被删除。

在第 491 行得到 php undefined offset 2 错误

这是我的模型:

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

在此视图页面中,用户和 user_role 显示在下拉列表中。

这是我的查看页面

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

【问题讨论】:

  • 请贴出相应的html。
  • 添加 dd($delete) 并在 $delete 中发布 Seesm 错误尝试检查 !empty($delete)
  • @tan 我已经在上面添加了相应的页面
  • @BRjava 在哪里添加 dd($delete)
  • @PradeepPathak 在初始化 $delete 后立即

标签: php codeigniter


【解决方案1】:

由于您使用 delete[] 作为数组,我希望您的视图处于循环中。 复选框值仅在选中时提交。所以你会以错误的顺序收到你的输入。 即,您选择了第一个用户和角色并检查了最后一个删除,您可能会收到第一个用户的删除。第二次和第三次删除将是未定义的。 Please see this link for details and hidden field hack for this issue

所以我建议在您的 html 中更改以下 3 行,如下所示。您可以使用 $participent[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">

从此改变循环

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

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

然后不是相等检查

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 

【讨论】:

    【解决方案2】:

    首先检查变量 $role 的数据量是否与 $user 相同,如果不是那么你就有问题了。

    接下来对于for($i=0;$i&lt;count($user);$i++) 行,为您的计数创建一个外部变量,这里每次您输入for 时,都会调用count(),优化不好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-01
      相关资源
      最近更新 更多