2016-05-05 29 views
1

我正在尋找更好的方式來遍歷一個對象並顯示其內容。
的FormController:更好的迭代顯示對象屬性PHP

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

感謝您的幫助提前!

+0

誰能幫幫我嗎? –

+0

當前設置的問題是什麼?你是否面臨任何錯誤?或者你想優化它? – Vincent

+0

@Vincent我得到一個錯誤,說**警告:爲foreach()提供的無效參數**它來自我實施的foreach。 –

回答

1

我看你已經設置的默認獲取模式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> 
+0

你說得對,當使用var_dump進行測試時,select_teacher_id()函數返回null。我已經意識到,由於某種原因,$ _SESSION ['login']沒有保留登錄ID,因爲它應該。另一方面,您展示元素的方式非常好。 –

+0

你可以這樣做,或者如果你希望它像早先那樣的對象,你可以傳遞PDO :: FETCH_OBJ標誌而不是FETCH_ASSOC,並且在視圖中相應地調用屬性,即使這樣也會做。 – Vincent

+1

做完了一些測試,我可以確認我的$ _SESSION ['登錄']是空的,你已經發現了。但是我不明白爲什麼我給$ _SESSION []的價值不再存在。我已經使用$ _POST表保存了一個用戶名。感謝您的幫助,我真的很感激! –