Ajoute un lien vers la news

main
Clément FRÉVILLE 2 years ago
parent 3ef74ce29f
commit 1bce756470

@ -24,7 +24,7 @@ class NewsGateway
*/ */
public function getPaginatedRecentNews(int $page = 1, int $limit = 10): array public function getPaginatedRecentNews(int $page = 1, int $limit = 10): array
{ {
$req = $this->pdo->prepare('SELECT title, LEFT(content, ' . self::EXCERPT_LENGTH . ') content, publication_date FROM news ORDER BY publication_date DESC LIMIT :limit OFFSET :offset;'); $req = $this->pdo->prepare('SELECT id_news, title, LEFT(content, ' . self::EXCERPT_LENGTH . ') content, publication_date FROM news ORDER BY publication_date DESC LIMIT :limit OFFSET :offset;');
$req->bindValue('limit', $limit, PDO::PARAM_INT); $req->bindValue('limit', $limit, PDO::PARAM_INT);
$req->bindValue('offset', ($page - 1) * $limit, PDO::PARAM_INT); $req->bindValue('offset', ($page - 1) * $limit, PDO::PARAM_INT);
if (!$req->execute()) { if (!$req->execute()) {
@ -61,6 +61,6 @@ class NewsGateway
private function createNews(array $data): News private function createNews(array $data): News
{ {
return new News($data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date'])); return new News(intval($data['id_news']), $data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']));
} }
} }

@ -8,17 +8,24 @@ use DateTime;
class News class News
{ {
private int $id;
private string $title; private string $title;
private string $content; private string $content;
private DateTime $publicationDate; private DateTime $publicationDate;
public function __construct(string $title, string $content, DateTime $publicationDate) public function __construct(int $id, string $title, string $content, DateTime $publicationDate)
{ {
$this->id = $id;
$this->title = $title; $this->title = $title;
$this->content = $content; $this->content = $content;
$this->publicationDate = $publicationDate; $this->publicationDate = $publicationDate;
} }
public function getId(): int
{
return $this->id;
}
public function getTitle(): string public function getTitle(): string
{ {
return $this->title; return $this->title;

@ -1,18 +1,20 @@
<?php $params['title'] = 'Home'; ?> <?php $params['title'] = 'Home'; ?>
<h1>Hello world!</h1> <h1>Hello world!</h1>
<?php foreach ($params['news'] as $news) : ?> <?php foreach ($params['news'] as $news) : ?>
<div class="card"> <a href="<?= $router->url('news/' . $news->getId()) ?>">
<header class="card-header"> <div class="card">
<p class="card-header-title"> <header class="card-header">
<?= $news->getTitle() ?> <p class="card-header-title">
</p> <?= $news->getTitle() ?>
</header> </p>
<div class="card-content"> </header>
<div class="content"> <div class="card-content">
<?= $news->getContent() ?>... <div class="content">
<?= $news->getContent() ?>...
</div>
</div> </div>
</div> </div>
</div> </a>
<?php endforeach ?> <?php endforeach ?>
<nav class="pagination" role="navigation" aria-label="pagination"> <nav class="pagination" role="navigation" aria-label="pagination">
<?php if ($params['page'] > 1) : ?> <?php if ($params['page'] > 1) : ?>

Loading…
Cancel
Save