2

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!

6
  • Could anyone help me ?
    – Joe Smith
    Commented May 5, 2016 at 13:33
  • what's the issue with the current set up ? are you facing any errors ? or you want to optimize it ? Commented May 5, 2016 at 13:37
  • @Vincent I get an error saying Warning: Invalid argument supplied for foreach() it is from the foreach I implemented.
    – Joe Smith
    Commented May 5, 2016 at 13:47
  • ok, are you using a PDO wrapper in your Db class ? Commented May 5, 2016 at 14:23
  • 1
    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 debug Commented May 5, 2016 at 14:58

1 Answer 1

2

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>
3
  • You were right, the select_teacher_id() function returns null when tested with a var_dump. I have realized that for some reason, $_SESSION['login'] is not holding the login id as it should. On the other hand, your way of displaying elements is very good.
    – Joe Smith
    Commented May 5, 2016 at 15:06
  • you can do it this way, or if you want it as objects like earlier, you can pass the PDO::FETCH_OBJ flag instead of FETCH_ASSOC, and in the view call the properties accordingly, even that would do.. Commented May 5, 2016 at 15:09
  • 1
    Having done some tests, i can confirm that my $_SESSION['login'] was empty, you were spot on. However I don't understand why the value that I gave to $_SESSION[] is not there anymore. I had saved a username using the $_POST table. Thanks for your help I really appreciate it!
    – Joe Smith
    Commented May 5, 2016 at 15:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.