0

I am getting a warning about "undefined offset = 0". What am I doing wrong?

This is my controller:

function document_data()
{
  $this->load->library('upload');

  $files = $_FILES;
  $cpt = count($_FILES['userfile']['name']);
  $tax_name=$this->session->userdata['doc_proff'];
  $tax_cid=array();

  foreach($tax_name as $key=>$value)
  {
    $tax_cid[]=$this->Tax_model->get_id($value);
  }

  foreach($tax_cid as $k=>$v) {
    $new[$k] = $v[0];
  }

  //var_dump($k);die('gugu');
  foreach($new as $k=>$v) {
    $tax_id[$k] = $v['tax_id'];
  }
  //var_dump($v);die('gugu');

  $this->upload->overwrite = true;

  for($i=0; $i<$cpt; $i++)
  {
    $ext=explode(".", $files['userfile']['name'][$i]);
    $file_name=$_POST["doc_title"][$i].".".$ext[1];                                                      
    $_FILES['userfile']['name']= $file_name;
    $_FILES['userfile']['type']= $files['userfile']['type'][$i];
    $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
    $_FILES['userfile']['error']= $files['userfile']['error'][$i];
    $_FILES['userfile']['size']= $files['userfile']['size'][$i];                    

    $empid=$this->session->userdata['empid'];
    $this->upload->initialize($this->set_upload_options());

    if ( ! $this->upload->do_upload())
    {
      $error = array('error' => $this->upload->display_errors());
      $this->load->view('doc_upload', $error);
    }

    $data=array(
      'doc_status' => 1,
      'doc_title'  => $_POST["doc_title"][$i],
      'doc_link'   => $file_name
    );

    $this->Tax_model->update_tax_submit($empid,$data,$tax_id[$i]);
    $this->session->set_flashdata('message', "<p>Data added successfully.</p>");
  }
}

This is my model:

public function get_id($name)
{
  $this->db->select('tax_id');
  $this->db->where('tax_name',$name);
  $query=$this->db->get('tax_saving');
  return $query->result_array();
}

The warning occurs on the following line of my controller:

$new[$k] = $v[0];

Everything seems to be working fine. I just want to get rid of this warning. What should I do?

Mischa
  • 42,876
  • 8
  • 99
  • 111
shiv223
  • 45
  • 2
  • 7
  • IF some one can explain what is offset it ll b great – shiv223 Aug 19 '13 at 06:28
  • you are never initiliazing the variable $new, so when you want to set a value for index $k, PHP warns/notices (dont know) that this offset does not exist. offset is the key value of an array – leuchtdiode Aug 19 '13 at 06:29
  • `Offset` means how far you are away from a certain point. For arrays, offset means how far you are away from the start of the array, hence 0 places , 1 place, 2 places, 3 places and so on. If you have an offset 0 being unavailable, that is not an array then – Hanky Panky Aug 19 '13 at 06:54
  • thank hanky panky.yes that is not an array,that is a string – shiv223 Aug 19 '13 at 06:59

2 Answers2

2

You already split index and value with $k => $v. Just use for get value: $v

foreach($tax_cid as $k => $v) {
  $new[$k] = $v;
}

You array dump:

array(4) 
{
 [0]=> array(0) { } 
 [1]=> array(1) { [0]=> array(1) { ["tax_id"]=> string(2) "10" } } 
 [2]=> array(1) { [0]=> array(1) { ["tax_id"]=> string(2) "11" } } 
 [3]=> array(1) { [0]=> array(1) { ["tax_id"]=> string(2) "14" } } 
} 

In fact, your $new[$k] = $v[0]; usage was right.

Cause of error, your first index is empty. Check your result from tax_model.

And why you use double foreach? You dont need below foreachs

  foreach($tax_cid as $k=>$v) {
    $new[$k] = $v[0];
  }

  //var_dump($k);die('gugu');
  foreach($new as $k=>$v) {
    $tax_id[$k] = $v['tax_id'];
  }

You can use something like with one foreach:

foreach($tax_name as $key=>$value)
{
   $result = $this->Tax_model->get_id($value);
   $tax_cid[] = $result[0]['tax_id'];
}
Bora
  • 10,529
  • 5
  • 43
  • 73
  • No bora im still getting that message. but if add your code i ll get error of undefined index tax_id – shiv223 Aug 19 '13 at 07:04
  • can you paste `var_dump($tax_cid)` to question `$tax_cid` array? – Bora Aug 19 '13 at 07:05
  • i am geting these values array(4) { [0]=> array(0) { } [1]=> array(1) { [0]=> array(1) { ["tax_id"]=> string(2) "10" } } [2]=> array(1) { [0]=> array(1) { ["tax_id"]=> string(2) "11" } } [3]=> array(1) { [0]=> array(1) { ["tax_id"]=> string(2) "14" } } } shiv – shiv223 Aug 19 '13 at 07:07
  • bora thnkx for the help buti did a mistake on my excel shit thts y i was getting error..now its fixed :D – shiv223 Aug 19 '13 at 08:37
0

It is showing error may be $v array does not have an index 0. First of all make sure that if $v is an array then use indexes and use appropriate indexes while assigning it and offset is index of an array.

K. V. Suresh
  • 897
  • 7
  • 8