Better iteration to display Object properties PHP
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:
<table>
<thead>
<tr>
<th width="20%" class="decoration">contact</th>
</tr>
</thead>
<tbody>
<?php foreach ($tabteachers as $teacher) { ?>
<tr>
<td>Lastname: <td>
<input type="text" name="lastnamepro" value="<?php echo $teacher->lastname_teacher() ?>" size="100px">
</tr>
<tr>
<td>Firstname: <td>
<input type="text" name="firstnamepro" value="<?php echo $teacher->firstname_teacher() ?>" size="100px">
</tr>
<tr>
<td>Email address: <td>
<input type="text" name="emailpro" value="<?php echo $teacher->email_teacher() ?>" size="100px">
</tr>
<tr>
<td>Service: <td>
<input type="text" name="servicepro" value="null" size="100px">
</tr>
<tr>
<td>mobile number: <td>
<input type="text" name="phonenumberpro" value="aucun numero" size="100px">
</tr>
<?php } ?>
</tbody>
</table><br>
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!
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
<table>
<thead>
<tr>
<th width="20%" class="decoration">contact</th>
</tr>
</thead>
<tbody>
<?php foreach ($tabteachers as $teacher) { ?>
<tr>
<td>Lastname: <td>
<input type="text" name="lastnamepro" value="<?php echo $teacher['lastname_teacher'] ?>" size="100px">
</tr>
<tr>
<td>Firstname: <td>
<input type="text" name="firstnamepro" value="<?php echo $teacher['firstname_teacher'] ?>" size="100px">
</tr>
<tr>
<td>Email address: <td>
<input type="text" name="emailpro" value="<?php echo $teacher['email_teacher'] ?>" size="100px">
</tr>
<tr>
<td>Service: <td>
<input type="text" name="servicepro" value="null" size="100px">
</tr>
<tr>
<td>mobile number: <td>
<input type="text" name="phonenumberpro" value="aucun numero" size="100px">
</tr>
<?php } ?>
</tbody>
</table><br>
链接地址: http://www.djcxy.com/p/69436.html
上一篇: PHP错误T
下一篇: 更好的迭代显示对象属性PHP