如何显示来自数据库的 class 之外的数据

How to display data outside of the class which is from database

我对如何显示来自 class 的数据感到困惑。

我有包含条目的数据库,我在 class:

中创建了一个 sql 请求
class Posts 
{

public function getPosts (  ) {

    $returnValue = FALSE;


    $query = "SELECT * FROM posts ORDER BY post_modified";

    try {
        $pdoCore = Core::getInstance();
        $pdoObject = $pdoCore->dbh->prepare($query);

        if ( $pdoObject->execute() ) {

            $pdoObject->setFetchMode(PDO::FETCH_ASSOC);


            while ( $posts_row = $pdoObject->fetch() ) {

                $this->posts[] = $posts_row;
            }

            $returnValue = TRUE;
        }           
    }
    catch ( PDOException $pe ) {
        trigger_error( ' Veritabanindan bilgiler alinamadi. ' . $pe->getMessage() );
    }

    return $returnValue;

} 
}

我正在尝试使用此代码显示它

$posts = new Posts();

$posts->getPosts();


foreach ($posts as $key => $value) {
    echo $value;
}

如您所见,我收到 Notice: Array to string conversion 错误,如果我将 $this->posts 声明为数组,则页面为空白。

你能解释一下在这种情况下应该如何检索数据吗?这种做法对吗?

谢谢。

你的问题

我假设您的 class 中有一个 posts 字段,而您在此处 post 没有(因为您确实以 $this->posts 访问它) .

在你的 foreach 循环中发生的事情是它遍历 Posts class 的所有字段并回应它们(这没有多大意义,但是没问题)。问题是 posts 不是一个字符串,而是一个数组,你不能回显数组。如果你使用 print_r 它应该会显示一些东西。

正在解决问题

您真正想要做的是 return getPosts 中的 post,然后对其进行迭代。所以基本上,return $this->posts(或者没有 post 的空数组,错误的例外),然后做这样的事情:

$posts = new Posts();

$posts2 = $posts->getPosts();

foreach ($posts2 as $key => $value) { // iterate over posts
    foreach ($value as $key2 => $value2) { // iterate over values of that post
    echo $value2;
}

一个可能更好的解决方案

请注意,这里的变量名很糟糕。这主要是因为你的posts对象不合常规。我希望 Post 对象包含 namelast_modified 等字段。Posts class 的字段包含 [=60] 的列表=]s 并没有多大意义(它只是 Posts 的列表,不需要 class)。

如果您想添加检索所有 post 的方法,您实际上不需要将结果列表存储在 class 中,只需 return Post 对象,然后你可以迭代它。

所以我会将您的代码更改为:

class Post {
    $name;
    $last_modified;

    // constructor

    // getters
}

class PostDAO {

    public function getAll() {
        // retrieve posts from db, return array of Post objects
    }
}

然后像这样使用它:

$postDAO = new PostDAO();
$posts = $postDAO->getAll();

foreach ($posts as $post) { // iterate over posts    
    echo $post->getName();
    echo $post->getLastModified();
}

优点是这样可以将 post 从数据库中检索的方式与代码的其余部分(例如显示 post )分离开来。

您的方法 getPosts() 将值分配给对象 $posts 的 属性 $posts。试一试:

$posts = new Posts();

$posts->getPosts();

foreach ($posts->posts as $key => $value) {
    echo $value;
}

在您的 foreach 中,$postsPosts class 的实例对象,因此尝试迭代 Posts 对象没有任何意义。相反,我会在 getPosts 方法和 return 中构建一个数组,而不是创建 posts 属性 和 returning 布尔值。

下面,$returnValue是得到returned的本地数组。

public function getPosts (  ) {

    $returnValue = array();

    $query = "SELECT * FROM posts ORDER BY post_modified";

    try {
        $pdoCore = Core::getInstance();
        $pdoObject = $pdoCore->dbh->prepare($query);

        if ( $pdoObject->execute() ) {

            $pdoObject->setFetchMode(PDO::FETCH_ASSOC);


            while ( $posts_row = $pdoObject->fetch() ) {

                $returnValue[] = $posts_row;
            }

        }           
    }
    catch ( PDOException $pe ) {
        trigger_error( ' Veritabanindan bilgiler alinamadi. ' . $pe->getMessage() );
    }

    return $returnValue;

} 

那么用法:

foreach ($posts->getPosts() as $key => $value) {
    echo $value['post_title'];
}