Jump to content

Recommended Posts

I get the below error and not sure what to look at to fix it.

 

 

Notice: Undefined offset: 2013 in customer_compare.php on line 124

Notice: Undefined offset: 2014 in customer_compare.php on line 125

Notice: Undefined offset: 2015 incustomer_compare.php on line 126

Notice: Undefined offset: 2016 in customer_compare.php on line 127

<?php
$begin=$_POST["begin"];	
$end=$_POST["end"];	


$connect =odbc_connect("Removed");
if(!$connect) {
	exit("Connection Failed: " . $connect);
}

$sql="
select 
*
 from

(SELECT DISTINCT 
	cmoOrganizationID
	,cmoName
	,ompOrderTotalBase
	,DATEPART(yyyy,ompOrderDate) as year
FROM m1_kf.dbo.SalesOrders
LEFT outer JOIN m1_kf.dbo.Organizations ON cmoOrganizationID = ompCustomerOrganizationID
LEFT outer JOIN m1_kf.dbo.Employees ON lmeEmployeeID = cmoAccountManagerEmployeeID

WHERE    ompClosed = - 1
and
(
		ompOrderDate >= '$begin'
		AND ompOrderDate <= '$end'
		)
	OR 
	(
		ompOrderDate >= convert(char(11),DATEADD(yy, -1, '$begin'),101)
		AND ompOrderDate <= convert(char(11),DATEADD(yy, -1,'$end'),101)
		)
		OR (
		ompOrderDate >= convert(char(11),DATEADD(yy, -2, '$begin'),101)
		AND ompOrderDate <= convert(char(11),DATEADD(yy, -2, '$end'),101)
		)
		
		OR (
		ompOrderDate >= convert(char(11),DATEADD(yy, -3, '$begin'),101)
		AND ompOrderDate <= convert(char(11),DATEADD(yy, -3, '$end'),101)
		)
		
		) as src
pivot
(
sum(ompOrderTotalBase)
  for year in ([2013], [2014], [2015] , [2016])
) piv

order by cmoOrganizationID
" ;
$result =odbc_exec($connect,$sql);
if(!$result){
exit("Error in SQL");
}

echo "<table><tr>";
echo "<th>CustID</th>";
echo "<th>Customer</th>";
echo "<th>2013</th>";
echo "<th>2014</th>";
echo "<th>2015</th>";
echo "<th>2016</th>";

//****************************************************************
 while ($row = odbc_fetch_array($result))
{
echo "<tr><td>" .$row['cmoOrganizationID'] ."</td>";
echo "<td>" .$row['cmoName'] ."</td>"; 
echo "<td>" .$row['2013'] ."</td>"; 
echo "<td>" .$row['2014'] ."</td>"; 
echo "<td>" .$row['2015'] ."</td>"; 
echo "<td>" .$row['2016'] ."</td></tr>"; 
 }
?>

Link to comment
https://forums.phpfreaks.com/topic/301470-notice-undefined-offset-2013/
Share on other sites

Your results will have a $row['year'] column.

 

2013, 2014 etc will be values in that column

 

EDIT: Sorry, didn't scroll down far enough to see the "pivot" keyword in your query.

Edited by Barand

Array
(
[cmoOrganizationID] => 10001
[cmoName] => custname removed
[2013] =>
[2014] =>
[2015] =>
[2016] => 20404.25
)
Array
(
[cmoOrganizationID] => 10002
[cmoName] =>  custname removed
[2013] => 4290.13
[2014] =>
[2015] =>
[2016] => 8768.67
)
Array
(
[cmoOrganizationID] => 10005
[cmoName] =>  custname removed
[2013] => 2499.68
[2014] => 5464.17
[2015] => 1431.86
[2016] => 4164.55
)

Edited by kat35601

I get the below error and not sure what to look at to fix it.

 

 

Notice: Undefined offset: 2013 in [/size]customer_compare.php on line [/size]124

 

Notice: Undefined offset: 2014 in [/size]customer_compare.php on line [/size]125

 

Notice: Undefined offset: 2015 in[/size]customer_compare.php on line [/size]126

 

Notice: Undefined offset: 2016 in [/size]customer_compare.php on line [/size]127

 

 

{
echo "<tr><td>" .$row['cmoOrganizationID'] ."</td>";
echo "<td>" .$row['cmoName'] ."</td>"; 
echo "<td>" .$row['year'] ."</td></tr>"; 
 }
Notice: Undefined index: year in [/size]customer_compare.php on line [/size]124

 

Comparing the error messages, the first error (2013) is "undefined offset" and the second (year) is "undefined index". So it looks as though 2013 is interpreted as a numeric column number instead of an alpha key.

did away with the Array

 

this fixed it.

 while (odbc_fetch_row($result)) {
	 
  $orgid=odbc_result($result,"cmoOrganizationID");
  $name=odbc_result($result,"cmoName");
  $tot2013=odbc_result($result,"2013");
  $tot2014=odbc_result($result,"2014");
  $tot2015=odbc_result($result,"2015");
  $tot2016=odbc_result($result,"2016");
 echo "<tr><td>$orgid</td>";
 echo "<td>$name</td>";
 echo "<td>$tot2013</td>";
 echo "<td>$tot2014</td>";
 echo "<td>$tot2015</td>";
 echo "<td>$tot2016</td>";

That's the puzzling thing - according to the last four lines in the code (initial post) they are quoted :confused:

I found it puzzling, too. I was too lazy to try from the database, but this is interesting:

 

<?php
error_reporting(-1);
ini_set("display.errors", 1);

/* 
	Test string indexes in array: http://forums.phpfreaks.com/topic/301470-notice-undefined-offset-2013/#entry1534467
*/

printf('PHP Version: %s' . PHP_EOL, phpversion());
printf('uname: %s' . PHP_EOL, php_uname());
echo PHP_EOL;

$res = array(
	array( 	'Row' => 'First',  (string) 2013 => 30.5, '2014' => 42, "2015" => 8 ),
	array( 	'Row' => 'Second', (string) 2013 => 11.5, '2014' => 42, "2015" => 6 ),
	);

foreach($res as $row) {
	printf('%6s:  %.2f  %d  %d' . PHP_EOL, $row['Row'], $row['2013'], $row['2014'], $row['2015']);
	printf('%6s:  %.2f  %d  %d' . PHP_EOL, $row['Row'], $row["2013"], $row["2014"], $row["2015"]);
	printf('%6s:  %.2f  %d  %d' . PHP_EOL, $row['Row'], $row[2013],   $row[2014],   $row[2015]);
	printf('%6s:  %.2f  %d  %d' . PHP_EOL, $row['Row'], $row[(string) 2013],   $row[(string) 2014],   $row[(string) 2015]);
	printf('%6s:  %.2f  %d  %d' . PHP_EOL, $row['Row'], $row[strval(2013)],   $row[strval(2014)],   $row[strval(2015)]);
	echo PHP_EOL;
}

echo PHP_EOL . 'SERIALIZED: ' . PHP_EOL;
echo serialize($res);
echo PHP_EOL;

echo PHP_EOL . 'VAR_DUMP: ' . PHP_EOL;
var_dump(array_keys($res[0]));
echo PHP_EOL;

/* RESULTS

PHP Version: 5.5.12
uname: Windows NT XXXXXX 6.1 build 7601 (Windows 7 Business Edition Service Pack 1) i586

 First:  30.50  42  8
 First:  30.50  42  8
 First:  30.50  42  8
 First:  30.50  42  8
 First:  30.50  42  8

Second:  11.50  42  6
Second:  11.50  42  6
Second:  11.50  42  6
Second:  11.50  42  6
Second:  11.50  42  6


SERIALIZED: 
a:2:{i:0;a:4:{s:3:"Row";s:5:"First";i:2013;d:30.5;i:2014;i:42;i:2015;i:8;}i:1;a:4:{s:3:"Row";s:6:"Second";i:2013;d:11.5;i:2014;i:42;i:2015;i:6;}}

VAR_DUMP: 
array(4) {
  [0] =>
  string(3) "Row"
  [1] =>
  int(2013)
  [2] =>
  int(2014)
  [3] =>
  int(2015)
}

*/
The different types of string quoting/coercing, makes no difference.

 

PHP is implicitly converting the associative keys to integers. Which does not matter when I reference them because of the loose data-typing in PHP.

 

My guess is that the database driver is setting the array keys (internal representation) as strings. The "solution" works (I guess) because the odbc->result() call expects a column name to be a string. The array reference (in the OP) was converted to an integer since that's how PHP (internally) handles them (when the key is composed of only digits).

 

 

The other interesting thing is: var_dump() on this machine, would not dump $res. Or I should say, it produced

array(2) {
  [0] =>
  int(0)
  [1] =>
  int(1)
}

I don't use var_dump() much - I usually use print_r() - unless I need to see the datatypes. So, maybe that's normal?
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.