2

Today I come with a logical problem.

I have a class that gives me complete information of the user. I've created a method that allows me to recover all the data of each user how return me an object.

Here is the method :

public static function getAllUsers(&$rUsersData = null, $sArgs = null) {

        // Nouvelle connexion à la base de données
        self::$oDatabase = new cMySQLi(DB_USERNAME, DB_PASSWORD, DB_HOSTNAME, DB_HOSTPORT, DB_DATABASE);

        // Si on ne passe pas de ressource en paramètre
        if(is_null($rUsersData)) {

            // Récupère les données de tout les users, actifs ou non
            $sReqSelectAllUsers = ' SELECT
                                        *
                                    FROM
                                        '.self::$sTableName.(is_null($sArgs) ? ' '.$sArgs.' ' : null).'
                                    ORDER BY
                                        '.self::$sFieldFirstname.' ASC';

            // Exécute la requête
            $rUsersData = self::$oDatabase->Query($sReqSelectAllUsers);
        }

        // Associe les données dans un tableau associatif à 2 dimensions
        $aUsersData = self::$oDatabase->Assoc($rUsersData);

        // Parcours chaque utilisateurs
        while($aData = $aUsersData) {

            // Retourne un objet avec les données de l'user
            return new cUser($aData[self::$sFieldId]);
        }

    }

And here is the call that I use to recover all data :

while($aUser = cUser::getAllUsers()) {

    print_r($aUser);
}

Here is my cMySQLi class if it can also help you : https://github.com/SatanicGeek/S.A.V-Team-Manager/blob/master/www/classes/mysqli.class.php

Could you help me to get all the users without any param in the method call (I can get of the users if I pass a sql ressource to my method) ?

1 Answer 1

1

Inside your getAllUsers() method you have a while loop that returns; this will only return a single user (return exits the loop & function, returning the value to the caller).

Since you want to return an array of all the users, instead use code similar to this:

$listOfUsers = [];

foreach($aUsersData as $aData) {
  $listOfUsers[] = new cUser($aData);
}
return $listOfUsers;

This way when you call the code, you'll have an array of user objects, not just a single user object.

For example to call this method, you would now use code like:

$allUsers = cUser::getAllUsers();
foreach($allUsers as $user) {
  print_r($user);
}

Technically this could be solved with using yield and making getAllUsers() as a generator but this would have horrible implications around the MySQL connection

Sign up to request clarification or add additional context in comments.

1 Comment

It's a possibility but that's not what I want. I'm training with PHP. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.