Welcome Guest, Not a member yet? Register   Sign In
DB $query->result_array() loop issues
#1

[eluser]zscott[/eluser]
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?

Code:
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?
#2

[eluser]sixpack434[/eluser]
I got the same problem, did you find a fix?
#3

[eluser]mironcho[/eluser]
Can you check $enrollCount value before second loop or var_dump $enrollArr array. Probably $enrollCount is 0.
#4

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

Quote: $query2 = $CI->db->query("SELECT * FROM ClassOffering WHERE classID = '".$enrollArr['enrollID'][$x]."'");

should be

Quote: $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.




Theme © iAndrew 2016 - Forum software by © MyBB