Welcome Guest, Not a member yet? Register   Sign In
Undefined Index
#1

(This post was last modified: 05-13-2017, 01:33 PM by cupboy1.)

How can I debug an undefined index problem? I've done everything I can think of to debug this.

The good code and the bad code look the same to me. The data looks the same.  Here is all the code. Maybe someone sees something I don't. I've spent hours on this. See the bottom box of Code.

In the meantime I've done a sloppy fix. It works but I don't know why I couldn't do it the other way. Here's the fix:
Code:
     if (isset($ddjuris)) {
       try {
         foreach ($ddjuris as $x) :
           // show be 15 rows or more
           $count1++;
           for ($i = 0; $i < count($x); $i++) {        
           $idx = $x[$i]['shortjuris'];
           if (trim($x[$i]['ddcase']) != '')
             $options[$idx] = $x[$i]['ddcase'];  // add key value pair to array ($idx is the key)      
             }  
         endforeach;
         log_message("info", "case view ddjuris items count: $count1");
       }
       catch (Exception $e) {
         log_message("info", "case view error: ".$e->getMessage());
       }
     }
     else
       log_message("info","case view could not find variable \$ddjuris or it was empty");




Code:
Good Code vs. Bad Code

bad code:

 function ddjuris() {
   $ddjuris = funcGetJurisDD();    
   $results['ddjuris'] = $ddjuris;   //  shortjuris, ddcase
   return $results;
 }  
   
     $count1 = 0;
     if (isset($ddjuris)) {
       try {
         foreach ($ddjuris as $x) :
           // show be 15 rows or more
           $count1++;
           $x = (array)$x;
           $idx = $x['shortjuris'];       // Message: Undefined index: shortjuris
           if (trim($x['ddcase']) != '')  // Message: Undefined index: ddcase
             $options[$idx] = $x['ddcase'];  // add key value pair to array ($idx is the key)        
         endforeach;
         log_message("info", "case view ddjuris items count: $count1");
       }
       catch (Exception $e) {
         log_message("info", "case view error: ".$e->getMessage());
       }
     }
     
     
good code:

   $formsdd = funcGetFormsDD();    
   $data['formsdd'] = $formsdd;
   return $data;

   $options = array(); // create a php array
   $idx = -1;
   $options[$idx] = "Select";
   if (isset($formsdd)) {
     foreach($formsdd as $x) :
       $x = (array)$x;
       $idx = $x['form_value'];
       $options[$idx] = $x['form_text'];
       //echo $options[$idx];
     endforeach;
     

function funcGetFormsDD() {
 $retval = false;
 $val = 0;
 $mysqli = ConnectToDB();
 $ddforms = array();
 $sql = "call getformsdd"; //  form_value, form_text
 log_message("info"," functions/funcGetJuris SQL = $sql ");
 try {

   if ($q = $mysqli->query($sql)) {
      $countRows = $q->num_rows;
        /* Fetch a result row as an associative array */
      while ($row = $q->fetch_assoc()) {        
        $ddforms[] = $row;  
      }
      if ($mysqli->more_results())
        $mysqli->next_result();        
      /* free result set */
      $q->free();    
     }
   }
   catch (Exception $ex)
   {
     echo $ex->getMessage();
     return true;
   }  

 $mysqli->close();
 log_message("info"," functions/funcGetFormsDD returned: ".print_r($ddforms, TRUE));
 return $ddforms;
}    

function funcGetJurisDD() {
 $retval = false;
 $val = 0;
 $mysqli = ConnectToDB();
 $ddjuris = array();
 $sql = "call getjurisdd"; //  shortjuris, ddcase
 log_message("info"," functions/funcGetJurisDD SQL = $sql ");
 $countRows = -1;
 try {

   if ($q = $mysqli->query($sql)) {
      $countRows = $q->num_rows;
        /* Fetch a result row as an associative array */
      while ($row = $q->fetch_assoc()) {        
        $ddjuris[] = $row;  
      }
      if ($mysqli->more_results())
        $mysqli->next_result();        
      /* free result set */
      $q->free();    
     }
   }
   catch (Exception $ex)
   {
     echo $ex->getMessage();
     return true;
   }  

 $mysqli->close();  
 log_message("info"," functions/funcGetJurisDD returned: ".print_r($ddjuris, TRUE));
 return $ddjuris;
}  

INFO - 2017-05-13 13:48:35 -->  functions/funcGetJurisDD returned: Array
(
   [0] => Array
       (
           [shortjuris] => dgd
           [ddcase] => DaggettD
       )

   [1] => Array
       (
           [shortjuris] => dgj
           [ddcase] => DaggettJ
       )



)

INFO - 2017-05-13 14:07:54 -->  functions/funcGetFormsDD returned: Array
(
   [0] => Array
       (
           [form_value] => information
           [form_text] => Information
       )

   [1] => Array
       (
           [form_value] => motion_continue
           [form_text] => Motion: Continue
       )


)
Reply


Messages In This Thread
Undefined Index - by cupboy1 - 05-13-2017, 01:20 PM
RE: Undefined Index - by InsiteFX - 05-13-2017, 02:19 PM
RE: Undefined Index - by PaulD - 05-13-2017, 03:43 PM



Theme © iAndrew 2016 - Forum software by © MyBB