Skip to content
Snippets Groups Projects
Commit 8e902533 authored by Silas Meister's avatar Silas Meister
Browse files

Create NewsArticle-Page to fetch articles by id

parent 55b72ad4
Branches
Tags
No related merge requests found
...@@ -77,7 +77,7 @@ function createNewsChild(news) { ...@@ -77,7 +77,7 @@ function createNewsChild(news) {
a.appendChild(textNode); a.appendChild(textNode);
const hrefAttr = document.createAttribute('href'); const hrefAttr = document.createAttribute('href');
hrefAttr.value = '#'; hrefAttr.value = 'NewsArticle.php?articleId=' + encodeURIComponent(news['id']);
a.setAttributeNode(hrefAttr); a.setAttributeNode(hrefAttr);
......
...@@ -40,13 +40,15 @@ class Index extends Page { ...@@ -40,13 +40,15 @@ class Index extends Page {
$news[] = new News($title, $imgurl, $content); $news[] = new News($title, $imgurl, $content);
} }
$resultSet->close();
return $news; return $news;
} }
protected function generateView():void protected function generateView():void
{ {
$this->news = $this->getViewData(); $this->news = $this->getViewData();
$this->generatePageHeader('NASA'); $this->generatePageHeader('NASA', '../functions.js');
echo <<<EOT echo <<<EOT
......
<?php declare(strict_types=1);
require_once 'Page.php';
class NewsArticle extends Page
{
private const ARTICLE_ID = 'articleId';
private const TITLE = 'title';
private const IMGURL = 'imgurl';
private const CONTENT = 'content';
private int $articleId;
protected function __construct()
{
parent::__construct();
$this->articleId = -1;
session_start();
}
public function __destruct()
{
parent::__destruct();
}
protected function getViewData():array
{
$stmt = $this->database->prepare("SELECT n.title, n.imgurl, n.content FROM news n WHERE n.id = ?");
$stmt->bind_param("i", $this->articleId);
$stmt->execute();
$resultSet = $stmt->get_result();
if ($resultSet->num_rows == 0)
return array();
$article = $resultSet->fetch_assoc();
$array = array(
self::TITLE => $article[self::TITLE],
self::IMGURL => $article[self::IMGURL],
self::CONTENT => $article[self::CONTENT]
);
$resultSet->free();
$stmt->close();
return $array;
}
protected function generateView():void
{
$data = $this->getViewData();
$this->generatePageHeader('News Article');
var_dump($data);
echo <<<EOT
<body>
<header></header>
</body>
EOT;
$this->generatePageFooter();
}
protected function processReceivedData():void
{
parent::processReceivedData();
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if (isset($_GET[self::ARTICLE_ID]) && is_numeric($_GET[self::ARTICLE_ID])) {
$this->articleId = intval($_GET[self::ARTICLE_ID]);
}
}
}
public static function main():void
{
try {
$page = new NewsArticle();
$page->processReceivedData();
$page->generateView();
} catch (Exception $e) {
header("Content-type: text/html; charset=UTF-8");
echo $e->getMessage();
}
}
}
NewsArticle::main();
\ No newline at end of file
...@@ -5,6 +5,7 @@ require_once 'util/constants.php'; ...@@ -5,6 +5,7 @@ require_once 'util/constants.php';
class NewsFetch extends Page class NewsFetch extends Page
{ {
private const ID = "id";
private const TITLE = "title"; private const TITLE = "title";
private const SEARCH_WORD = "searchWord"; private const SEARCH_WORD = "searchWord";
...@@ -31,15 +32,16 @@ class NewsFetch extends Page ...@@ -31,15 +32,16 @@ class NewsFetch extends Page
$likeSearchWord = "%" . $this->searchWord . "%"; $likeSearchWord = "%" . $this->searchWord . "%";
$stmt = $this->database->prepare("SELECT n.title FROM news n WHERE LOWER(n.title) LIKE LOWER(?) LIMIT 5"); $stmt = $this->database->prepare("SELECT n.id, n.title FROM news n WHERE LOWER(n.title) LIKE LOWER(?) LIMIT 5");
$stmt->bind_param("s", $likeSearchWord); $stmt->bind_param("s", $likeSearchWord);
$stmt->execute(); $stmt->execute();
$resultSet = $stmt->get_result(); $resultSet = $stmt->get_result();
while($row = $resultSet->fetch_assoc()) { while($row = $resultSet->fetch_assoc()) {
$id = $row[self::ID];
$title = $row[self::TITLE]; $title = $row[self::TITLE];
$data[] = array(self::TITLE => $title); $data[] = array(self::ID => $id, self::TITLE => $title);
} }
return $data; return $data;
......
...@@ -25,7 +25,7 @@ abstract class Page { ...@@ -25,7 +25,7 @@ abstract class Page {
/*$this->database->close();*/ /*$this->database->close();*/
} }
protected function generatePageHeader(string $title = ""):void protected function generatePageHeader(string $title = "", string $jsScript = ""):void
{ {
$title = htmlspecialchars($title); $title = htmlspecialchars($title);
header("Content-type: text/html; charset=UTF-8"); header("Content-type: text/html; charset=UTF-8");
...@@ -37,7 +37,11 @@ abstract class Page { ...@@ -37,7 +37,11 @@ abstract class Page {
<title>$title</title> <title>$title</title>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<script src="../functions.js"></script> EOT;
if (!empty(trim($jsScript)))
echo "<script src=\"$jsScript\"></script>";
echo <<<EOT
<link rel="stylesheet" style="text/css" href="../styles.css" /> <link rel="stylesheet" style="text/css" href="../styles.css" />
</head> </head>
EOT; EOT;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment