0

我不知道如何将数据库中的每个用户分配为类属性并显示它们。看一看。例如,我的数据库中有这张表:

+----+----------+------------+-----------+
| id | username | first_name | last_name |
+----+----------+------------+-----------+
|  1 | guu      |  Guu       | GuuShtein |
|  3 | user1    |  Allan     | Last      |
|  4 | Donny    |  Name      | Last Name |
+----+----------+------------+-----------+

这是课程:

class users {

public $db;
private $id;
private $username;
private $first_name;
private $last_name;

public function __construct($db) {
    $this->db=$db;
}

private function find_by_id($id) {
   $record = $this->db->query('SELECT * FROM users WHERE id=' . $id .' LIMIT 1')->fetch(PDO::FETCH_ASSOC);     
   foreach ($record as $atribute => $value) {
       $this->$atribute = $value;
   } 
}

public function user_nfo($id){
    $this->find_by_id($id);

    $user_nfo = "User id: " . $this->id . "<br />";
    $user_nfo .= "Username: " . $this->username . "<br />";
    $user_nfo .= "User full name: " . $this->first_name . " " . $this->last_name . "<br />";       
    return $user_nfo;
}
}

$users = new mysql_a($db);
echo $users->user_nfo(1);

我的问题是如何编写一个方法,逐个显示所有用户?我的意思是类似于我的 user_nfo 函数,但仅适用于所有记录?我尝试使用 PDOfetchAll 进行“foreach”循环,但它只是覆盖属性并且只显示最后一条记录。

4

2 回答 2

2

您想要的是一个名为 user 的新对象,它具有您想要的所有属性,如下所示。

<?php

try {
    $db = new PDO('mysql:host=localhost;dbname=sub_db;charset=UTF-8', DB_USER, DB_PASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
} catch (PDOException $ex) {
    echo "error";
}

class users {

    public $db;

    public function __construct($db) {
        $this->db=$db;
    }

    /**
     * Fetch all users and return an array of their mapped objects
     *
     * @author Daniel Noel-Davies
     *
     * @return array
     */
    public function getAllUsers () {
        $users   = array();

        // Grab an array of each user
        $users = $this->db->query('SELECT * FROM users')->fetch(PDO::FETCH_ASSOC);

        // Loop through the users
        foreach( $users as $index => $user ) {
            // Replace each row with an object of that user (keeps memory down)
            $users[$index] = new User($user);
        }

        // return our array containing an object of each user
        return $users;
    }

    public function count_row(){
        $count = $this->db->query('SELECT COUNT(*) FROM users')->fetch(PDO::FETCH_ASSOC);
        return $count['COUNT(*)'];
    }

    public function find_by_id($row) { 
       $record = $this->db->query('SELECT * FROM users LIMIT 1 OFFSET ' . $row)->fetch(PDO::FETCH_ASSOC);
       return $record;
    }   
 }


class user {

    public $id;
    public $username;
    public $first_name;
    public $last_name;

    public function __construct($record){    
           foreach ($record as $atribute => $value) {
               $this->$atribute = $value;
           }
    }
}

// Initiate our users class
$objUsers = new Users;

// Fetch all the users
$users = $objUsers->getAllUsers();

// Loop through them all
foreach ($users as $user) {
    // Using Printf keeps things neater, means you're template is tidier.
    printf(
        '%s<br />
        %s<br />
        %s<br />
        %s<br />',

        $user->id,
        $user->username,
        $user->first_name,
        $user->last_name
    );
}
?>

这有帮助吗?

于 2012-10-19T09:33:02.523 回答
0

感谢“NoelDavies”的回答,这是我设法为我工作的完整代码。如果有人遇到同样的问题:

<?php

try {
$db = new PDO('mysql:host=localhost;dbname=sub_db;charset=UTF-8', DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
} catch (PDOException $ex) {
    echo "error";
}

class mysql_a {

    public $db;

    public function __construct($db) {
        $this->db=$db;
    }

    public function count_row(){
        $count = $this->db->query('SELECT COUNT(*) FROM users')->fetch(PDO::FETCH_ASSOC);
        return $count['COUNT(*)'];
    }

    public function find_by_id($row) { 
       $record = $this->db->query('SELECT * FROM users LIMIT 1 OFFSET ' . $row)->fetch(PDO::FETCH_ASSOC);
       return $record;
    }   
 }


class user {

    public $id;
    public $username;
    public $first_name;
    public $last_name;

    public function __construct($record){    
           foreach ($record as $atribute => $value) {
               $this->$atribute = $value;
           }
    }
}


$users = new mysql_a($db);

for ($i = 0; $i < $users->count_row(); $i++) {
    $user = new user($users->find_by_id($i));
    echo $user->id         . "<br />";
    echo $user->username   . "<br />";
    echo $user->first_name . "<br />";
    echo $user->last_name  . "<hr />";
}
?>

它计算 db 中的行数,然后将它们一一拉出并输出。我确信有一些更短更优雅的方式,但它现在对我来说很好;] 再次感谢“NoelDavies”。

于 2012-10-19T21:18:05.837 回答