Cofem/lib/newsarticle.php
2024-02-22 13:57:05 +01:00

61 lines
1.8 KiB
PHP

<?php
function getNewsArticles() :array
{
global $mysqli;
$output = ["Status" => "Fail"]; // Default Status is "Fail"
$articles = [];
if (isLoggedIn()) {
$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; ");
$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 $output;
}
function addNewsArticle($title="Nazov", $body="Obsah") :array
{
global $mysqli;
$output = ["Status" => "Fail"]; // Default Status is "Fail"
if (isLoggedIn()) {
$query = $mysqli->prepare("INSERT INTO NewsArticles (WrittenBy, Title, Body, FileList) VALUES (?, ?, ?, 0);");
$query->bind_param("iss", $_SESSION["id"], htmlspecialchars($title), htmlspecialchars($body));
$query->execute();
if ($query->affected_rows > 0) {
$output["Status"] = "Success";
}
}
$query->close();
return $output;
}