更好的迭代显示对象属性PHP

我正在寻找更好的方式来遍历一个对象并显示其内容。
的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函数():

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;
        }
    }

表单视图:

    <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连接:

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;
    }

感谢您的帮助提前!


我发现你已经将默认的获取模式设置为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>
链接地址: http://www.djcxy.com/p/69435.html

上一篇: Better iteration to display Object properties PHP

下一篇: Joomla Parse error: syntax error, unexpected T