Reputation: 7207
I have this php code which throws the alert notice: undefined offset
$thisMonth=$_POST['month']; //the value is today's date's month
$thisYear=$_POST['year']; //the value is today's date's year
$thisDay=$_POST['day']; //the value is today's date's day
$table=mysql_query("SELECT * FROM `kids` WHERE `debt`!='0'") or die(mysql_error());
$debt=0;
while($row=mysql_fetch_assoc($table)){
$explodedDate=explode('/',$row['enrollmentdate']);
$theYear=$explodedDate[0];
$theMonth=$explodedDate[1]; //this line throws the error
$theDay=$explodedDate[2]; //and also this line
if((int)$theYear==(int)$thisYear && (int)$theMonth==(int)$thisMonth){
if((int)$theDay==(int)$thisDay || (int)$thisDay==0){
$debt+=$row['debt'];
}
}
}
I have been reading all over the internet for a solution but seems like this error is dependent on the code and unfortunately I can't seem to figure out how to fix it.
any ideas how to fix the error or what's causing it?
this is the full error:
Notice: Undefined offset: 1 in C:\wamp\www\kids_house\php\functions.php on line 600 Notice: Undefined offset: 2 in C:\wamp\www\kids_house\php\functions.php on line 601
Upvotes: 2
Views: 1953
Reputation: 136
The reason that these two lines are throwing errors is because the value in this is not yyyy/mm/dd, as you expect:
$explodedDate=explode('/',$row['enrollmentdate']);
If you look at the value that throws the error with something like this, you should see the issue:
$thisMonth=$_POST['month']; //the value is today's date's month
$thisYear=$_POST['year']; //the value is today's date's year
$thisDay=$_POST['day']; //the value is today's date's day
$table=mysql_query("SELECT * FROM `kids` WHERE `debt`!='0'") or die(mysql_error());
$debt=0;
while($row=mysql_fetch_assoc($table)){
$explodedDate=explode('/',$row['enrollmentdate']);
if ( count( $explodedDate ) <= 1 ) {
var_dump( $row ); //this will show you the row that is causing the notice
var_dump( $explodedDate ); //this will show you the date
die();
}
$theYear=$explodedDate[0];
$theMonth=$explodedDate[1]; //this line throws the error
$theDay=$explodedDate[2]; //and also this line
if((int)$theYear==(int)$thisYear && (int)$theMonth==(int)$thisMonth){
if((int)$theDay==(int)$thisDay || (int)$thisDay==0){
$debt+=$row['debt'];
}
}
}
If you want to retry with commas for lines with an enrollment date formatted like yyyy,mm,dd you could do this. It's not the most elegant solution, but it sounds like you have dirty data so may have to.
$thisMonth=$_POST['month']; //the value is today's date's month
$thisYear=$_POST['year']; //the value is today's date's year
$thisDay=$_POST['day']; //the value is today's date's day
$table=mysql_query("SELECT * FROM `kids` WHERE `debt`!='0'") or die(mysql_error());
$debt=0;
while($row=mysql_fetch_assoc($table)){
$explodedDate=explode('/',$row['enrollmentdate']);
//try again with commas
if ( count( $explodedDate ) == 0 ) {
$explodedDate=explode(',',$row['enrollmentdate']);
}
//skip the record if still no enrollment date
if ( count( $explodedDate ) == 3 ) {
$theYear=$explodedDate[0];
$theMonth=$explodedDate[1]; //this line throws the error
$theDay=$explodedDate[2]; //and also this line
if((int)$theYear==(int)$thisYear && (int)$theMonth==(int)$thisMonth){
if((int)$theDay==(int)$thisDay || (int)$thisDay==0){
$debt+=$row['debt'];
}
}
}
}
Upvotes: 2