未定义的偏移量:1甚至使用空()

时间:2017-09-30 09:58:01

标签: php

所以我在我的复选框中放了2个值,第一个是金额,第二个是id。我已经使用了empty()但仍然无法解决这个“Undefined offset”问题。有什么建议吗?

 <input type="checkbox"  name="checkbox[]" value="1::<?php echo $players->player_id?>">
 <input type="checkbox"  name="checkbox2[]" value=".5::<?php echo $players->player_id?>">
 <input type="checkbox"  name="checkbox3[]" value=".5::<?php echo $players->player_id?>">
 <input type="checkbox"  name="checkbox4[]" value="-.5::<?php echo $players->player_id?>">           

在我的控制器中

public function update() {
    for ($i = 0; $i < count($this->input->post('checkbox')); $i++) {
        $checkboxvalue = explode('::', $this->input->post('checkbox')[$i]);
        $checkboxvalue2 = explode('::', $this->input->post('checkbox2')[$i]);
        $checkboxvalue3 = explode('::', $this->input->post('checkbox3')[$i]);
        $checkboxvalue4 = explode('::', $this->input->post('checkbox4')[$i]);
        if(empty($checkboxvalue[0])){
            $checkboxvalue[0] = 0;
        }
        if(empty($checkboxvalue[0])){
            $checkboxvalue2[0] = 0;
        }
        if(empty($checkboxvalue[0])){
            $checkboxvalue3[0] = 0;
        }
        if(empty($checkboxvalue[0])){
            $checkboxvalue4[0] = 0;
        }
        $totals = $checkboxvalue[0] + $checkboxvalue2[0] + $checkboxvalue3[0] + $checkboxvalue4[0];
        $data = array(
            'player_id' => $checkboxvalue[1],
            'player_att1' => $checkboxvalue[0],
            'player_att2' => $checkboxvalue2[0],
            'player_att3' => $checkboxvalue3[0],
            'player_att4' => $checkboxvalue4[0],
            'player_atotal' => $totals,
        );
        $this->load->model('Evaluation_model');
        $this->Evaluation_model->editview($checkboxvalue[1], $data);
        $checkboxvalue = array();
    }
    redirect(base_url('Evaluations'));
}

非常感谢帮助。

PS: enter image description here

1 个答案:

答案 0 :(得分:1)

只有选中的复选框才会发送到服务器。因此,每个复选框数组将具有不同数量的元素,具体取决于检查这些框的数量。您无法使用count($this->input->post('checkbox'))作为所有复选框的限制。

您需要为每个复选框使用单独的循环。您可以使用键入播放器ID的关联数组来收集每个复选框中的数据。

public function update() {
    $players = array();

    foreach ($this->input->post('checkbox') as $checkbox) {
        list($value, $player_id) = explode('::', $checkbox);
        self::update_player($players, $player_id, 'player_att1', $value);
    }
    foreach ($this->input->post('checkbox2') as $checkbox) {
        list($value, $player_id) = explode('::', $checkbox);
        self::update_player($players, $player_id, 'player_att2', $value);
    }
    foreach ($this->input->post('checkbox3') as $checkbox) {
        list($value, $player_id) = explode('::', $checkbox);
        self::update_player($players, $player_id, 'player_att3', $value);
    }
    foreach ($this->input->post('checkbox4') as $checkbox) {
        list($value, $player_id) = explode('::', $checkbox);
        self::update_player($players, $player_id, 'player_att4', $value);
    }
    $this->load->model('Evaluation_model');
    foreach ($players as $player_id => $data) {
        $this->Evaluation_model->editview($player_id, $data);
    }
    redirect(base_url('Evaluations'));
}

static function update_player(&$players, $id, $property, $value) {
    if (!isset($players[$id])) {
        $players[$id] = array(
            'player_id' => $id,
            'player_att1' => 0,
            'player_att2' => 0,
            'player_att3' => 0,
            'player_att4' => 0,
            'player_atotal' => 0
        );
    }
    $players[$id][$property] = $value;
    $players[$id]['player_atotal'] += $value;
}