我正在寻找一种更好的方法来遍历对象并显示其内容
窗体控制器:
公共函数run(){$tabteachers=";if(!empty($_SESSION['apply'])&&!空($_SESSION["应用程序"]){$tabtears=DB::getInstance()->select_tacher_id($_SESSION['login']);}require_one(VIEW_PATH.'formdeux.php');}
Db.class函数():
公共函数select_tacher_id($login){$query='从教师、实习、学生中选择*WHERE teachers.email_tacher=interships.email_reresponsible_internship-AND-students.registry_number_student=interships.registry_student_enterships.registry_student_entership='$this->_db->报价($login);$result=$this->_db->query($query);if($result->rowcount()!=0){while($row=$result->fetch()){$teacher[]=新教师($row->email_tacher,$row->firstname_tacher、$row->lastname_tacheer,$row->responsible_internship_tacher为NULL);}return$tear;}}
表单视图:
<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连接:
私有静态$instance=null;私有$db;私有函数__construct(){尝试{$this->_db=新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('连接错误:'.$e->getMessage());}}//Pattern Singleton公共静态函数getInstance(){if(is_null(self::$instance)){self::$instance=new Db();}return self::$instance;}
提前感谢您的帮助!
我看到你已经将默认的获取模式设置为fetch_OBJ,在这种特殊的情况下,尽管我认为你可以使用PDO的fetchAll方法将整个结果集作为一个数组,然后迭代它,
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;
}
}
//查看
<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>