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
#2

Is the array index starting at 0 or 1, because the count1 is being incremented before the value is assigned.

I would check to see what the starting index value is and go from there.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 05-13-2017, 03:47 PM by PaulD.)

The way I would debug an undefined index, if it was not immediately obvious from inspection of the code, would be to do some testing on the function alone. So firstly output the array you are looping through and check it has the data in it you expected. Then remove the loop functionality and add in some testing lines to output the index you are looking for within your data set. Usually these things show the problem quite clearly. You should then easily be able to see where in the loop you are looking for an index that does not exist.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB