1

I have the following piece of code which joins two tables and then returns all those users from users table who are teachers.

The users who qualifies to be teachers, information is then retrieved from the teachers table

So assuming there are 3 users who qualifies as teachers from the users table, all 3 those users with there information should be stored in an array.

The array gets empties when page loads

SQL STATEMENT

//IF my reasoning is correct the folllowing sql query should select alll users who qualifies as teachers
$sql = "SELECT users.*, teachers.*
        FROM users INNER JOIN teachers ON teachers.userID = users.userID";

FUNCTION

public function showAllTeachers()
    {
        $db = DB::getInstance();
        $sql = "SELECT users.*, teachers.*
        FROM users INNER JOIN teachers ON teachers.userID = users.userID";

        //simple DB QUERY
        $stmnt = $db->prepare($sql);
        $stmnt->execute();
        $teachers = $stmnt->fetchAll();
        $teacherInfo = array();

        //Check if there are any users who qualifies as teachers else return fals
        if ($stmnt->rowCount() > 0) {
            foreach($teachers as $teacher){
          //Store all teacher info in array $teacherInfo
          //There are more than 1 teacher so loop shouldl iterate more than once

                $teacherInfo = array('name' =>$teacher['firstname'], 'headline' =>$teacher['headline'], 'location' => $teacher['location'],
                    'age' => $teacher['age'], 'experience' => $teacher['experience'], 'imgPath' => $teacher['imgPath']);
            }//foreach

            //return array with teacher info
            return $teacherInfo;
        }//rowcount
        else {
            return false; //no teachers
                }
    }//function

Since there are more than 1 user who qualifies as a teacher $teacherInfo should now contain data for all those teachers

Emptying Array

The following runs when page is loaded and should display all teachers.

My Problem When emptying array $allTeachersI only get one result (teacher) back. Thus my loop is only iterating once for some reason

 $teachers = new TeacherSearch();
           $allTeachers = $teachers->showAllTeachers();
            if(is_array($allTeachers)){
                foreach($allTeachers as $key => $teacher){
                    echo $key;
                    echo $teacher;
                    echo '<br />';
                }
              }

Any help much appreciated

Additional Info

Database

enter image description here

Teachers Table

enter image description here

Users Table

enter image description here

Users Table Data

enter image description here

4
  • $teacherInfo = array(...) <= you reinitialize your value in each loop ;) Commented Jul 21, 2017 at 7:44
  • @PierreGranger Actually, $teacherInfo = array(); is outside of the loop, hence $teacherInfo is initialized once only (which is the desired behaviour). The problem comes from the missing brackets after $teacherInfo in the statement $teacherInfo = array('name'....., which overwrites the variable instead of appending an array to it. Commented Jul 21, 2017 at 7:48
  • I'm talking about the one in the loop, the $teacherInfo = array('name'...), not the one before the loop. Commented Jul 21, 2017 at 7:49
  • Right, I guess I misunderstood your comment, sorry about that. Commented Jul 21, 2017 at 7:51

1 Answer 1

6

It doesn't seem to be a problem of your loop executing once only, add echo 'test' inside it, and you'll see it is executed multiple times (as long as $teachers has more than one element, of course).

The problem is you're overwriting $teacherInfo on each of your foreach loop execution, instead of appending a new array to it.

Replace :

$teacherInfo = array('name' =>$teacher['firstname'], 'headline' =>$teacher['headline'], 'location' => $teacher['location'],
    'age' => $teacher['age'], 'experience' => $teacher['experience'], 'imgPath' => $teacher['imgPath']);

With :

$teacherInfo[] = array('name' =>$teacher['firstname'], 'headline' =>$teacher['headline'], 'location' => $teacher['location'],
    'age' => $teacher['age'], 'experience' => $teacher['experience'], 'imgPath' => $teacher['imgPath']);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank You for your answer, the problem I am having now is that I get an Array to String Conversion Error when adding the advised [] brackets
The error occurs here $allTeachers = $teachers->showAllTeachers(); When I do a print_r it shows the array with all correct values` However when I try to loop with $allTeachers I get above mentioned error
The Array to String Conversion Error comes from your echo $teacher; in your second script. $teacher is an array, you need to implode it or loop through it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.