I'm confused how to display data from class.
I have database with entries and I created a sql request inside of a class:
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;
}
}
and I'm trying to display it with this code
$posts = new Posts();
$posts->getPosts();
foreach ($posts as $key => $value) {
echo $value;
}
as you notice I get Notice: Array to string conversion
error and if I declare $this->posts as an array the page is blank.
Can you explain me how should retrieve data in this situation. is this approach right?
thanks.