0

I have the following php code that is looking for the last entry of test entry in a mysql query result.

It checks if the last entry is valid for a device or it tries to search the latest one in the for that type of test (or failing that leave it as untested). After that, it does the same for the second device in the same manner. However I get errors pointing to lines on second foreach loop.

  if ($device1_valid) {
$Results_d1 = $History[$TestNo][$iter]['Results_d1'];
$Colour_d1 = Colour($Results_d1);
$Date_d1    = $History[$TestNo][$iter]['Date_'];
  } else {
  foreach ($History[$TestNo]['iter'] as $item) {
       $device1_valid = $History[$TestNo][$item]['d1_valid'];
       if ($sf1_valid) {
         $Results_d1 = $History[$TestNo][$item]['Results_d1'];
         $Colour_d1 = Colour($Results_d1);
         $Date_d1    = $History[$TestNo][$item]['Date_'];
         break;
        } else {
        $Results_d1 = "----";
        $DateTime_d1 ="----";
        $Colour_d1 = 'white';
        }
      }
  }
  unset($item);


  if ($device2_valid) {
$Results_d2 = $History[$TestNo][$iter]['Results_d2'];
$Colour_d2 = Colour($Results_d2);
$Results_d2 = $History[$TestNo][$iter]['Results_d2'];
$Date_d2    = $History[$TestNo][$iter]['Date_'];
   } else {
  foreach ($History[$TestNo]['EntryNo'] as $item) {
       $device2_valid = $History[$TestNo][$item]['d2_valid'];
       if ($device2_valid) {
         $Results_d2 = $History[$TestNo][$item]['Results_d2'];
         $Colour_d2 = Colour($Results_d2);
         $Date_d2    = $History[$TestNo][$item]['Date_'];
         break;
        } else {
        $Results_d2 = "----";
        $DateTime_d2 ="----";
        $Colour_d2 = 'white';
        }
      }

This results in warnings for the second loop as such:

Notice: Undefined index: EntryNo in /server/filename.php on line 129
Warning: Invalid argument supplied for foreach() in /server/filename.php on line 129

Why is this error occurring and how will I be able to remove it? The query does result in correct data (which is displayed later but I don't understand why theses notifications and warning are happening. This only happens in the second foreach loop and not the first.

Edit:

$History[$TestNo] is a multidimensional array.... so vardump gives array(49) { [0]=> array(25) {....} [1]=> array(25) [2]=> array(25){...} etc. I call this function setting $EntryNo to 0.

vardump $History[$TestNo][$EntryNo] simply gives array(25) {....}

There are no warnings in the first loop but second loop it says the index is undefined. This is key reason why the other question identified as duplicate does not address my issue. The question is why is this occuring in the second foreach loop and how can I avoid this.

Umar
  • 121
  • 1
  • 10
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Epodax Jun 21 '16 at 09:38
  • I don't understand if EntryNo is not set then why the error does not occur on the first foreach loop. – Umar Jun 21 '16 at 09:41

1 Answers1

2

For `Notice: Undefined index: EntryNo in /server/filename.php on line 129 Warning: Invalid argument supplied for foreach() in /server/filename.php on line 129'

It must be like this at foreach($History[$TestNo]['EntryNo'] as $item) There is no element in array $History[$TestNo] with with key EntryNo. Would you please var_dump($History[$TestNo]) and check it ?

Notice: Undefined variable: Colour_sf2 in /server/filename.php on line 184 For this, You have not included enough code here but it must be because you have not defined $Colour_sf2 before use it in any function or condition.

Maulik Kanani
  • 642
  • 6
  • 22
  • Again why does this not occur in the first foreach loop case? – Umar Jun 21 '16 at 09:42
  • for execute loop `$History[$TestNo]['EntryNo']` has to be array and exist. Would you please check for that ? If its not exist, you need to change your entire code. – Maulik Kanani Jun 21 '16 at 09:46
  • Its a multidimensional array.... so vardump `$History[$TestNo]['EntryNo']` gives `array(49) { [0]=> array(25) {....} [1]=> array(25) [2]=> array(25){...}` etc. I call this function setting $EntryNo to 0. There are no warnings in the first loop but second loop it says the index is undefined. – Umar Jun 21 '16 at 12:18