0

I have created a query in codegniter for updating data in the form of array when I am update the the data with 3 fields it's working fine but when i try to add 4th field and then I try to update data it says undefined offset 3 | undefined offset 4 don't know why is this happening

public function update_content() {
    $i = 0;
    foreach($this->input->post() as $val):
        $heading = $this->input->post('heading')[$i];
        $span    = $this->input->post('span')[$i];
        $id      = $this->input->post('id')[$i];
        $type    = $this->input->post('type')[$i];
        $data = array(
            'heading' => $heading,
            'span'    => $span,
            'type'    => $type
        );
        $this->db->where('id', $id);
        $this->db->update('contentpage', $data);
        $i++;
    endforeach;
}

Here is my html

<input type="text" name="heading[]" size="20" value="<?php echo $data->heading; ?>" />
<input type="text" name="span[]" size="20" value="<?php echo $data->span; ?>" />
<input type="hidden" name="id[]" size="20" value="<?php echo $data->id; ?>" />
<input type="hidden" name="type[]" value="Default" />
Talha
  • 903
  • 8
  • 31
Usman Khan
  • 359
  • 1
  • 13

2 Answers2

1

Try this coding ...

public function update_content() {

    for($i = 0; $i < count($this->input->post('heading')); $i++) {
        $heading = $this->input->post('heading')[$i];
        $span    = $this->input->post('span')[$i];
        $id      = $this->input->post('id')[$i];
        $type    = $this->input->post('type')[$i];
        $data = array(
            'heading' => $heading,
            'span'    => $span,
            'type'    => $type
        );
        $this->db->where('id', $id);
        $this->db->update('contentpage', $data);

   }
}

Otherwise replace below coding

 foreach($this->input->post() as $val):

to

 foreach($this->input->post('heading') as $val):
msvairam
  • 862
  • 5
  • 12
0

find size of array and for loop that times

$heading = $this->input->post('heading');
$span    = $this->input->post('span');
$id      = $this->input->post('id');
$type    = $this->input->post('type');
$count = sizeof($id);
for($i=0; $i<$count; $i++):
  if( $heading[$i]==''):
    $data = array(
        'heading' => $heading[$i],
        'span'    => $span[$i],
        'type'    => $type[$i]
    );
    $this->db->where('id', $id[$i]);
    $this->db->update('contentpage', $data);
  endif;
endfor;
swaroop suthar
  • 632
  • 3
  • 19