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
Teachers Table
Users Table
Users Table Data




$teacherInfo = array();is outside of the loop, hence$teacherInfois initialized once only (which is the desired behaviour). The problem comes from the missing brackets after$teacherInfoin the statement$teacherInfo = array('name'....., which overwrites the variable instead of appending an array to it.