From 4b288b48c8a5b0959ac314052f67cde8b9343c40 Mon Sep 17 00:00:00 2001 From: bruno Date: Thu, 22 Feb 2024 13:57:05 +0100 Subject: [PATCH] Test --- assets/script.js | 20 +++++++++++++------ lib/newsarticle.php | 38 ++++++++++++++++++++++++++++++------- pages/news/index.php | 24 ++++++++++++----------- templates/newsArticle.html | 10 ++++++++++ templates/newsArticles.html | 5 +++++ 5 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 templates/newsArticle.html diff --git a/assets/script.js b/assets/script.js index 4077d14..f3a6ced 100644 --- a/assets/script.js +++ b/assets/script.js @@ -179,16 +179,24 @@ async function togglearticlecreate(){ articleContainerElement.classList.toggle("hidden"); } +async function renderarticle(articledata){ + +} + async function submitarticle(){ let articleTitleElement = document.getElementById("articletitleinput"); let articleBodyElement = document.getElementById("articlebodyinput"); - await doAction("/newsarticle", { - action: "addNewsArticle", - title: articleTitleElement.value, - body: articleBodyElement.value - }, "Článok úspešne pridaný", + await doAction( + "/newsarticle", + { + action: "addNewsArticle", + title: articleTitleElement.value, + body: articleBodyElement.value + }, + "Článok úspešne pridaný", "Nastala chyba pri pridávaní článku", - false); + false + ); } async function articleInit(){ diff --git a/lib/newsarticle.php b/lib/newsarticle.php index bc26a6e..08d02c5 100644 --- a/lib/newsarticle.php +++ b/lib/newsarticle.php @@ -4,19 +4,43 @@ function getNewsArticles() :array { global $mysqli; + $output = ["Status" => "Fail"]; // Default Status is "Fail" + $articles = []; if (isLoggedIn()) { - $result = $mysqli->query("SELECT NewsArticles.ID, NewsArticles.WrittenAt, NewsArticles.WrittenBy, NewsArticles.Title, NewsArticles.Body, NewsArticles.FileList, Users.Nickname FROM NewsArticles INNER JOIN Users ON NewsArticles.WrittenBy = Users.ID; "); + $stmt = $mysqli->prepare("SELECT NewsArticles.ID, NewsArticles.WrittenAt, NewsArticles.WrittenBy, NewsArticles.Title, NewsArticles.Body, NewsArticles.FileList, Users.Nickname FROM NewsArticles INNER JOIN Users ON NewsArticles.WrittenBy = Users.ID; "); - // Check if the query executed Successfully - if ($result) { - while ($row = $result->fetch_assoc()) { - $articles[] = $row; - } + $id = 0; + $writtenAt = ""; + $writtenBy = 0; + $title = ""; + $body = ""; + $filelist = 0; + $writtenByName = ""; + + $stmt->bind_result($id, $writtenAt, $writtenBy, $title, $body, $filelist, $writtenByName); + + $stmt->execute(); + + while ($stmt->fetch()) { + $articles[] = [ + 'ID' => $id, + 'WrittenAt' => $writtenAt, + 'Title' => $title, + 'Body' => $body, + 'WrittenByName' =>$writtenByName + ]; } + + // Check if any results were fetched + if (!empty($articles)) { + $output["Status"] = "Success"; + $output["Articles"] = $articles; + } + } - return $articles; + return $output; } function addNewsArticle($title="Nazov", $body="Obsah") :array diff --git a/pages/news/index.php b/pages/news/index.php index 528549a..164b122 100644 --- a/pages/news/index.php +++ b/pages/news/index.php @@ -8,8 +8,13 @@ global $routerConfig; $output = file_get_contents($routerConfig["template_dir"] . "newsArticles.html"); $articles_out = ""; -$articles = getNewsArticles(); +$articles = []; +$articles_tmp = getNewsArticles(); +if($articles_tmp['Status'] == "Success"){ + $articles = $articles_tmp["Articles"]; +} +$articleTemplate = file_get_contents($routerConfig["template_dir"] . "newsArticle.html"); foreach ($articles as $article){ $articleTitle = htmlspecialchars($article["Title"]); $articleBody = htmlspecialchars($article["Body"]); @@ -18,18 +23,15 @@ foreach ($articles as $article){ $articleWrittenAt = htmlspecialchars($article["WrittenAt"]); $articleWrittenByName = htmlspecialchars($article["Nickname"]); - $articles_out .= "
-

$articleTitle

-
-

$articleWrittenByName

-

$articleWrittenAt

-
-
-$articleBody -
-
"; + $articleTemplate = str_replace("__TEMPLATE_ARTICLE_TITLE__", $articleTitle, $articleTemplate); + $articleTemplate = str_replace("__TEMPLATE_ARTICLE_AUTHOR__", $articleWrittenByName, $articleTemplate); + $articleTemplate = str_replace("__TEMPLATE_ARTICLE_DATE__", $articleWrittenAt, $articleTemplate); + $articleTemplate = str_replace("__TEMPLATE_ARTICLE_BODY__", $articleBody, $articleTemplate); + + $articles_out .= $articleTemplate; } +$output = str_replace("__TEMPLATE_FOR_ARTICLE_CONTENT__", $articleTemplate, $output); $output = str_replace("__TEMPLATE__ARTICLES_HERE__", $articles_out, $output); return [ diff --git a/templates/newsArticle.html b/templates/newsArticle.html new file mode 100644 index 0000000..c6b009b --- /dev/null +++ b/templates/newsArticle.html @@ -0,0 +1,10 @@ +
+

__TEMPLATE_ARTICLE_TITLE__

+
+

__TEMPLATE_ARTICLE_AUTHOR__

+

__TEMPLATE_ARTICLE_DATE__

+
+
+ __TEMPLATE_ARTICLE_BODY__ +
+
\ No newline at end of file diff --git a/templates/newsArticles.html b/templates/newsArticles.html index 1de4ac0..ea74d38 100644 --- a/templates/newsArticles.html +++ b/templates/newsArticles.html @@ -4,6 +4,11 @@
+ + +
__TEMPLATE__ARTICLES_HERE__