0

I am getting the following php notice : Notice: Undefined variable: NextCourse in C:\Program Files (x86)\EasyPHP-5.3.3\www\exam\createMap 1.php on line 10

Notice: Undefined offset: 0 in C:\Program Files (x86)\EasyPHP-5.3.3\www\exam\createMap 1.php on line 14

Notice: Undefined offset: -1 in C:\Program Files (x86)\EasyPHP-5.3.3\www\exam\createMap 1.php on line 15

Notice: Undefined offset: 1 in C:\Program Files (x86)\EasyPHP-5.3.3\www\exam\createMap 1.php on line 16

Notice: Undefined offset: 0 in C:\Program Files (x86)\EasyPHP-5.3.3\www\exam\createMap 1.php on line 17

Notice: Undefined offset: 0 in C:\Program Files (x86)\EasyPHP-5.3.3\www\exam\createMap 1.php on line 18

Notice: Undefined variable: NextCourse in C:\Program Files (x86)\EasyPHP-5.3.3\www\exam\createMap 1.php on line 10

this is the piece of code related for the notice:

require_once("MySQLConnection.php");
function get_next_course ($CurrentCourse){
$arr_all_courses=array('CS11','CS112','CS113');
$arrlength = count($arr_all_courses);
for($i = 0; $i < $arrlength; $i++) {
if ($CurrentCourse==$arr_all_courses[$i])
$NextCourse=$arr_all_courses[$i+1];
}
return $NextCourse;
}
function CanSeat($i,$j,$CourseCode) {
$array_temp=array();
$curr_element=$array_temp[$i][$j];
$above_element=$array_temp[$i-1][$j];
$below_element=$array_temp[$i+1][$j];
$right_element=$array_temp[$i][$j+1];
$left_element=$array_temp[$i][$j-1];
    if ($curr_element==$above_element || $curr_element==$below_element || $curr_element==$right_element || $curr_element==$left_element)
        {$next_element=get_next_course($CourseCode);
    CanSeat($i,$j,$next_element);
        } else return true ;

}

and this is the loop calling the above function

for ($i=0;$i<$nbCol;$i++)
{
    for($j=0;$j<$nbRow;$j++)
     {
     $CurrentCourse=$arr_all_courses[$h];
     if (CanSeat($i,$j,$CurrentCourse))
     $arr_map[i][j]=$CurrentCourse;
     $h++;
     if($h==3)
     $h=0;
     }
     }

Any help ?!

  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Alma Do May 26 '15 at 12:51

1 Answers1

0

$NextCourse is created inside the if block. You should create it in the function body with a default value and then change the value inside the if block.

Also, it should be $i and $j in the array indices instead of i and j.

Note that the first time you call the function, 0-1 = -1.

ArthurTheLearner
  • 315
  • 4
  • 12
  • this fixed the error of line 10 , but the error still exist about offset in lines 15,16,17,18,19 is there something wrong in this code: function CanSeat($i,$j,$CourseCode) { $array_temp=array(); $curr_element=$array_temp[$i][$j]; $above_element=$array_temp[$i-1][$j]; $below_element=$array_temp[$i+1][$j]; $right_element=$array_temp[$i][$j+1]; $left_element=$array_temp[$i][$j-1]; – user3207053 May 27 '15 at 07:30