EllisLab text mark
Advanced Search
     
DB $query->result_array() loop issues
Posted: 30 July 2007 12:45 PM   [ Ignore ]
Avatar
Joined: 2006-10-12
7 posts

Hello,
I’m running the following code, it will find the enrollID’s but when looping through those it doesn’t seem to want to for-next the second query.. any suggestions?

case 'user':
                    
// return classes from given user id
                    
$query $CI->db->query("SELECT * FROM Enrollment WHERE studentID = '".$searchVar."'");
                    foreach (
$query->result_array() as $row{
                        $enrollArr[
'enrollID'][] $row['enrollID'];
                        
$enrollArr['classID'][] $row['classID'];
                        
$enrollArr['studentID'][] $row['studentID'];
                    
}
                    
//$query->free_result(); tried this.. nothing
                    // loop through all enrollments
                    
$enrollCount count($enrollArr['enrollID']);
                    for (
$x 0$x <= $enrollCount -1$x++) {
                        $query2 
$CI->db->query("SELECT * FROM ClassOffering WHERE classID = '".$enrollArr['enrollID'][$x]."'");
                        
                        foreach (
$query2->result_array() as $row2{
                            $tempClasses[
'classID'][] $row2['classID'];
                            
$tempClasses['className'][] $row2['className'];
                            
$tempClasses['CLASS_CD'][] $row2['CLASS_CD'];
                            
$tempClasses['Language'][] $row2['Language'];
                            
$tempClasses['Level'][] $row2['Level'];
                            
$tempClasses['Instructor'][] $row2['Instructor'];
                            
$tempClasses['Time'][] $row2['Time'];
                            
$tempClasses['Day'][] $row2['Day'];
                            
$tempClasses['Location'][] $row2['Location'];
                        
}
                    }
                    
return $tempClasses;
                    break; 

Is this a bug with result_array() being nested inside a loop?

 
Posted: 07 September 2007 08:33 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-09-06
32 posts

I got the same problem, did you find a fix?

 
Posted: 07 September 2007 10:50 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-04-18
120 posts

Can you check $enrollCount value before second loop or var_dump $enrollArr array. Probably $enrollCount is 0.

 
Posted: 08 September 2007 10:32 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2006-10-12
7 posts

I actually did find the fix, but only after a few hours of converting the code to standard PHP code to further debug it.

$query2 = $CI->db->query(“SELECT * FROM ClassOffering WHERE classID = ‘“.$enrollArr[‘enrollID’][$x].”’”);

should be

$query2 = $CI->db->query(“SELECT * FROM ClassOffering WHERE classID = ‘“.$enrollArr[‘classID’][$x].”’”);

I just had the wrong ID from enrollArr in my WHERE clause…........ I swear it’s always the simplest crap that gets me going.