This is my code for the method to fetch data from the database. I am passing those data as objects to use in html file.
* Get news details
*
* @return object
*/
public static function getNews()
{
$sql = 'SELECT * FROM news
ORDER BY date DESC';
$db = static::getDB();
$stmt = $db->prepare($sql);
$stmt->setFetchMode(PDO::FETCH_CLASS, get_called_class());
$stmt->execute();
$news = $stmt->fetch();
return $news;
}
Now, this is my rendering function for the index page.. getNews()
is method of News
class:
* Show the index page
*
* @return void
*/
public function indexAction()
{
$news = new News();
$news = $news::getNews();
View::renderTemplate('Home/index.html', [
'news' => $news
]);
}
After news object is got.. I wanted to add posts according to descending timestamp. The problem I got is that $news
is not taking data of whole table. It only shows data of latest object.
<div class="row">
{% for eachnews in news %}
<div class="col-4 mt-4">
<div class="card" style="width: 18rem;">
<img src="/uploads/news/{{ eachnews.fullfilename }}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{ eachnews.title }}</h5>
<p class="card-text">{{ eachnews.description }}</p>
<a href="news" class="btn btn-primary">Read More</a>
</div>
</div>
</div>
{% endfor %}
</div>
Every suggestion and answer are highly appreciated. Please ask me, if my problems are not clear.