这是从DB返回2个字段的正确方法吗?

时间:2014-10-23 02:03:59

标签: php mysql

我可以通过单独的视图和控制器获取Id的用户,但在这个实例中,我试图获取所有用户的列表并将其显示在屏幕上。问题出在getUserArray函数或getUser函数中。虽然我不够精通。 getUserById函数不会调用getUserArray()或getUser()并且它工作正常。在DB中没有名为“user”的字段,因为它现在只是我想要抓取并显示的SELECT语句中的3个字段。

    //UserDB.class.php
    public static function getAll() {
        $query =  "SELECT userId, userFirstName, userLastName 
                      FROM users
                      GROUP BY userId";

        $users = array();
        try {
            $db = Database::getDB ();
            $statement = $db->prepare($query);
            $statement->execute ();
            $users = UserDB::getUserArray($statement->fetchAll(PDO::FETCH_ASSOC));
            $statement->closeCursor ();
        } catch ( PDOException $e ) { // Not permanent error handling
            echo "<p>Error getting all users ".$e->getMessage()."</p>";
        }
        return $users;
    }

    public static function getUserArray($rowSets) {
        $users = array ();
        foreach ( $rowSets as $userRow ) {
            $user = UserDB::getUser($userRow);
            array_push ( $users, $user );
        }
        return $users;
    }

    public static function getUser($userRow) {
        // Return a comment object from a rowset.
        return new UserData ( $userRow );
    }



//UserData.class.php
// Responsibility: Holds data for comment and performs validation
// Constructor expects an associative array with field values for initialization
<?php
class UserData {
    private $userId;
    private $user;
    private $userFirstName;
    private $userLastName;


    public function __construct($formInput) {
        $this->initialize($formInput);
    }

    public function getUser() {
        return $this->user;
    }

    public function getUserFirstName(){
        return $this->userFirstName;
    }

    public function getUserLastName(){
        return $this->userLastName;
    }

    public function getUserId() {
        return $this->userId;
    }


    public function getParameters() {
        // Return data fields as an associative array
        $paramArray = array("userId" => $this->userId,
                            "userFirstName" => $this->userFirstName,
                            "userLastName" => $this->userLastName
                           );
        return $paramArray;
    }

    public function printUser() {
        echo "<h1>SHIELD Member</h1>";
        echo "User Id: $this->userId<br>";
        echo "User first name: $this->userFirstName<br>";
        echo "User last name: $this->userLastName<br>";
    }

    private function initialize($formInput) {
        if (isset($formInput['userId']))
            $this->userId = $formInput['userId'];
        else
            $this->userId = 0;
        if (isset($formInput['userFirstName']))
            $this->userFirstName = $formInput['userFirstName'];
        if (isset($formInput['userLastName']))
            $this->userLastName = $formInput['userLastName'];
    }
}
?>

//UsersController.php
<?php
include_once("../views/showUsers.php");
include_once("../models/UserDB.class.php");
include_once("../models/UserData.class.php");
include_once("../models/Database.class.php");


$myUsers = UserDB::getAll();
showUsers($myUsers, "All users");
?>




//showUsers.php
// Displays a list of all users 
// Input: an array of CommentData objects
<?php
function showUsers($userList, $msg) {
    echo "<h1>".$msg."</h1>";

    foreach ($userList as $user) {
        $user->printUser();
    }

    echo '<h3><a href="../index.php">Back to home</a>';
}
?>

0 个答案:

没有答案