I'm looking for a better way to iterate through an object and display its contents.
FormController:
public function run(){
$tabteachers='';
if(!empty($_SESSION['apply']) && !empty($_SESSION['application'])){
$tabteachers = DB::getInstance()->select_teacher_id($_SESSION['login']);
}
require_once(VIEW_PATH.'formdeux.php');
}Db.class function():
public function select_teacher_id($login){
$query = 'SELECT * FROM teachers,internships,students WHERE teachers.email_teacher = internships.email_responsible_internship AND students.registry_number_student = internships.registry_student_internship AND internships.registry_student_internship = '. $this->_db->quote ( $login );
$result = $this->_db->query ( $query );
if ($result->rowcount () != 0) {
while ( $row = $result->fetch () ) {
$teacher[]= new teacher ( $row->email_teacher,$row->firstname_teacher,$row->lastname_teacher,$row->responsible_internship_teacher,NULL);
}
return $teacher;
}
}Form View:
contact
Lastname:
Firstname:
Email address:
Service:
mobile number:
Db connection:
private static $instance = null;
private $_db;
private function __construct() {
try {
$this->_db = new PDO ('mysql:host=localhost;dbname=ProjectWeb;charset=utf8', 'root', '' );
$this->_db->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$this->_db->setAttribute ( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ );
} catch ( PDOException $e ) {
die ( 'Erreur de connexion à la base de donnée : ' . $e->getMessage () );
}
}
// Pattern Singleton
public static function getInstance() {
if (is_null ( self::$instance )) {
self::$instance = new Db ();
}
return self::$instance;
}
thanks for your help in advance!
Answer
I see you have set the default fetch mode to FETCH_OBJ , in this particular case though I think you can rather get the entire result set as an array using PDO's fetchAll method and then iterate though it,
public function select_teacher_id($login){
$query = 'SELECT * FROM teachers,internships,students WHERE
teachers.email_teacher = internships.email_responsible_internship
AND students.registry_number_student = internships.registry_student_internship
AND internships.registry_student_internship = '. $this->_db->quote ( $login );
$result = $this->_db->query ( $query );
if ($result->rowcount () != 0) {
//fetch the entire result set as a multi-dimensional assoc array
$teachers_result = $result->fetchAll(PDO::FETCH_ASSOC);
return $teachers_result;
}
}
// VIEW
contact
Lastname:
Firstname:
Email address:
Service:
mobile number:
No comments:
Post a Comment