2
votes

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!

1
Could anyone help me ?Joe Smith
what's the issue with the current set up ? are you facing any errors ? or you want to optimize it ?Vishnu Nair
@Vincent I get an error saying Warning: Invalid argument supplied for foreach() it is from the foreach I implemented.Joe Smith
ok, are you using a PDO wrapper in your Db class ?Vishnu Nair
See the answer below and If you are still seeing invalid argument error for foreach, In your controller please check if the method select_teacher_id() returns any data, use print_r() or var_dump() to debugVishnu Nair

1 Answers

2
votes

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>