$articleTitle
-- -
$articleWrittenAt
--
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 .= " $articleWrittenAt __TEMPLATE_ARTICLE_DATE__$articleTitle
-
-
-
-__TEMPLATE_ARTICLE_TITLE__
+
+
+
+