PHP Undefined Offset Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php001
Forum Newbie
Posts: 10
Joined: Tue Jul 03, 2012 12:19 pm

PHP Undefined Offset Error

Post by php001 »

Undefined offset: 1 in C:\xampp\htdocs\cms\supplierIDcrement.php on line 9

line 9:
list($one[0],$one[1]) = explode('EP', $EmployeeID);

Below is the previous problems
list($one[0],$one[1]) = split('EP', $EmployeeID);

Because of the split() can't be used in the Newest version of php, so I change to explode. The problems is solved, but pop out this problems again. Who can tell what's the problems? What is the meaning of undefined offset?
php001
Forum Newbie
Posts: 10
Joined: Tue Jul 03, 2012 12:19 pm

Re: PHP Undefined Offset Error

Post by php001 »

Notice: Undefined offset: 1 in C:\xampp\htdocs\cms\supplierIDcrement.php on line 9
What the undefined offset means? I checked the $employeeID = $row["employeeID"]; , this is same as the column name.

Code: Select all

<?php
include("connect.php");

$query = "SELECT employeeID, number FROM employee ORDER BY number DESC LIMIT 1";
$result = $db->query($query);

$row = $result->fetch_assoc();
$employeeID = $row["employeeID"];
[color=#FF0000]list($one[0],$one[1]) = explode('EP', $employeeID);
[/color]
$index = ++$one[1];
$number = $index;

if($index < 10)
{
	$index = "EP000".$index;
}
else if($index < 100)
{
	$index = "EP00".$index;
}
else if ($index < 1000)
{
	$index = "EP0".$index;
}
else if($index < 10000)
{
	$index = "EP".$index;
}
else
{
	$index = "Your ID field is full!";
}

echo "<input type='hidden' name='EmployeeID' value='".$index."'><input type='hidden' name='number' value='".$number."'>".$index;
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP Undefined Offset Error

Post by Christopher »

You are exploding the string, but it is not creating an array with two elements. The array probably only has one element. You need to do something like this to guarantee that you have two elements:

Code: Select all

$one = explode('EP', $employeeID);
if (!isset($one[1])) {
     $one[1] = '';
}
(#10850)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Undefined Offset Error

Post by social_experiment »

php001 wrote:What the undefined offset means?
I came across 'undefined offset' last night; in my case it was in an array - i tried to access $array[19] when the array only had 19 elements meaning the last element would be $array[18]. In short it is trying to access a value (in both cases in an array) that isn't defined.

What value is contained in $employeeID?

Code: Select all

<?php
 $employeeID = $row["employeeID"];
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
php001
Forum Newbie
Posts: 10
Joined: Tue Jul 03, 2012 12:19 pm

Re: PHP Undefined Offset Error

Post by php001 »

$row["employeeID"]; is the employeeID from the table of employee, this is the column name and also primary key.
php001
Forum Newbie
Posts: 10
Joined: Tue Jul 03, 2012 12:19 pm

Re: PHP Undefined Offset Error

Post by php001 »

But there is no any record in my table of employee, is that my logic error?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Undefined Offset Error

Post by social_experiment »

php001 wrote:But there is no any record in my table of employee, is that my logic error?
If there is no value for $row["employeeID"] (equal to '') then it will cause the error you are receiving. A brief example

Code: Select all

<?php
 $name = 'FieldEPZero'; # yields -> array(2) { [0]=> string(5) "Field" [1]=> string(4) "Zero" }
 $name = 'Field'; # yields -> array(1) { [0]=> string(5) "Field" } - gives undefined offset error because there is no second element.
 $name = ''; # yields -> array(1) { [0]=> string(0) "" } - gives undefined offset error because there is no second element.
$value = explode('EP', $name);
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply