Kate Kate - 3 years ago 184
PHP Question

Undefined offset :1 even using empty()

So I put 2 values in my checkbox,the first one is the amount and the second one is the id. I already use empty() but still I cant solve this "Undefined offset" problem. Any suggestion?

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


And in my contoller

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


Help is very much appreciated.

PS:
enter image description here

Answer Source

Only checked boxes get sent to the server. So each checkbox array will have different numbers of elements depending on how many of those boxes are checked. You can't use count($this->input->post('checkbox')) as the limit for all the checkboxes.

You need to use a separate loop for each checkbox. And you can use an associative array keyed off the player IDs to gather the data from each checkbox.

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;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download