6 Replies - 1104 Views - Last Post: 17 August 2011 - 10:42 PM Rate Topic: -----

#1 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Undefined offset error

Posted 17 August 2011 - 12:07 AM

$rowCount=0;
$x=0;
for ($j = 1; $j <= $reader->sheets[0]["numRows"]; $j++){
		$dev = $reader->sheets[0]["cells"][$j+1][1];
		$dev2 = $reader->sheets[0]["cells"][$j][1];
	if($dev!=$dev2){
			$data1[] = $reader->sheets[0]["cells"][$j][1];
			$data2[] = "";
			$data3[] = "";
			$data4[] = "";
			$data5[] = $reader->sheets[0]["cells"][$j][4].$reader->sheets[0]["cells"][$j][6];
	}
			$data = array(
		array($data1[$j-1],$data2[$j-1],$data3[$j-$j-1],$data4[$j-1],$data5[$j-1])
		);
	$x++;
	echo $x;
	foreach ($data as $row) {
	foreach ($row as $key => $value) {
    $sheet->write($rowCount, $key, $value);    
		}  
	$rowCount++;
	
	}
		
}



help...this one always gets me undefined offset...

i thinks the problem is in here
$data = array(
		array($data1[$j-1],$data2[$j-1],$data3[$j-$j-1],$data4[$j-1],$data5[$j-1])
		);


but i really can't figure it out..

Is This A Good Question/Topic? 0
  • +

Replies To: Undefined offset error

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,565
  • Joined: 05-June 09

Re: Undefined offset error

Posted 17 August 2011 - 01:04 AM

That whole bit of code makes no sense and, from what I can see, wouldn't work.

Basically it's saying that the key $j - 1 isn't set in the $data[0-5] arrays.
Was This Post Helpful? 0
  • +
  • -

#3 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Re: Undefined offset error

Posted 17 August 2011 - 01:38 AM

what would you suggest to do...i tried to removed the "-1" still more undefined offset shows
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,565
  • Joined: 05-June 09

Re: Undefined offset error

Posted 17 August 2011 - 02:25 AM

Do you understand what your code is doing and/or PHP?

It's not the most difficult of errors to figure out, and if you wrote this code in the first place you would see the reasons for it happening.
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4238
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Undefined offset error

Posted 17 August 2011 - 02:45 AM

Just look at the numbers. On the first iteration of the loop PHP will calculate the array indexes to look like this:
$data = array(
    array($data1[0],$data2[0],$data3[-1],$data4[0],$data5[0])
);


The 0 indexes may be valid, but the $data3[-1] index doesn't make a lot of sense. Granted, you don't actually show us the code that initializes that array (or any of them, for that matter) but unless you specifically set a -1 element, it won't be there.

Are you sure you didn't mean: $data3[$j-1]? Doing $j-$j-1 is obviously an error. (Why put $j in the mix just to remove it again?)
Was This Post Helpful? 1
  • +
  • -

#6 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Re: Undefined offset error

Posted 17 August 2011 - 05:43 PM

View PostRudiVisser, on 17 August 2011 - 02:25 AM, said:

Do you understand what your code is doing and/or PHP?

It's not the most difficult of errors to figure out, and if you wrote this code in the first place you would see the reasons for it happening.



you are supposed to help...why bragging? if you don't want to help then don't just post stupid suggestions...NOT all people are like you!!!
Was This Post Helpful? 0
  • +
  • -

#7 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,565
  • Joined: 05-June 09

Re: Undefined offset error

Posted 17 August 2011 - 10:42 PM

View Postneverender2488, on 18 August 2011 - 01:43 AM, said:

you are supposed to help...why bragging? if you don't want to help then don't just post stupid suggestions...NOT all people are like you!!!

No, the point is that we are here to help you with your code, not something that's copy/pasted.

The arrays $data1-5 are not required as you're not using them for anything apart from setting up a row in $data (which is also a single array'd array so your multiple for loops are not required).

The whole thing can be refactored, changing $data to an actual array of rows like your loops expect, and moving those loops out of the main for loop as below (untested):
$rowCount = 0;
$x = 0;
$data = array();
for ($j = 1; $j <= $reader->sheets[0]["numRows"]; $j++) {
    $dev = $reader->sheets[0]["cells"][$j+1][1];
    $dev2 = $reader->sheets[0]["cells"][$j][1];
	if ($dev != $dev2){
        $tmpRow = array();
		$tmpRow[] = $reader->sheets[0]["cells"][$j][1];
		$tmpRow[] = "";
		$tmpRow[] = "";
		$tmpRow[] = "";
		$tmpRow[] = $reader->sheets[0]["cells"][$j][4] . $reader->sheets[0]["cells"][$j][6];
        $data[] = $tmpRow;
	}
	$x++;
	echo $x;
}

foreach ($data as $row) {
    foreach ($row as $key => $value) {
        $sheet->write($rowCount, $key, $value);
    }
    $rowCount++;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1