get post from API
This commit is contained in:
parent
5a31509318
commit
f8e37db21f
|
@ -4,6 +4,7 @@ require("./imgPreview");
|
||||||
require("./includeHTML");
|
require("./includeHTML");
|
||||||
require("./ntpGraph");
|
require("./ntpGraph");
|
||||||
require("./ntpMenu");
|
require("./ntpMenu");
|
||||||
|
require("./viewPost");
|
||||||
|
|
||||||
require("./customElements/cookie");
|
require("./customElements/cookie");
|
||||||
require("./customElements/svgLoader");
|
require("./customElements/svgLoader");
|
||||||
|
|
40
js/viewPost.js
Normal file
40
js/viewPost.js
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
if(window.location['pathname'] == "/post.html"){
|
||||||
|
loadPost();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function getParameter(key) {
|
||||||
|
|
||||||
|
// Address of the current window
|
||||||
|
let address = window.location.search
|
||||||
|
|
||||||
|
// Returns a URLSearchParams object instance
|
||||||
|
let parameterList = new URLSearchParams(address)
|
||||||
|
|
||||||
|
// Returning the respected value associated
|
||||||
|
// with the provided key
|
||||||
|
return parameterList.get(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadPost() {
|
||||||
|
let id = getParameter("id");
|
||||||
|
|
||||||
|
let header = document.createElement("jl-header");
|
||||||
|
let footer = document.createElement("jl-footer");
|
||||||
|
let content = document.createElement("div");
|
||||||
|
|
||||||
|
if(id == null) {
|
||||||
|
content.innerHTML = "<h1>404 - Post not found</h1>";
|
||||||
|
} else {
|
||||||
|
let post = await (await fetch("/API/getPost.php?id=" + id)).json();
|
||||||
|
content.innerHTML = post["content"];
|
||||||
|
document.title = post["title"] + " - Jonas Leder";
|
||||||
|
|
||||||
|
header.setAttribute("data-title", post["title"]);
|
||||||
|
}
|
||||||
|
content.id = "content";
|
||||||
|
|
||||||
|
document.body.appendChild(header);
|
||||||
|
document.body.appendChild(content);
|
||||||
|
document.body.appendChild(footer);
|
||||||
|
}
|
24
public/API/getPost.php
Normal file
24
public/API/getPost.php
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
include "./lib/config.php";
|
||||||
|
include "./lib/mysql.php";
|
||||||
|
|
||||||
|
$id = $conn->real_escape_string($_GET["id"]);
|
||||||
|
$result = $conn->query("SELECT * FROM posts WHERE id=$id");
|
||||||
|
if ($result->num_rows > 0) {
|
||||||
|
$row = $result->fetch_assoc();
|
||||||
|
} else {
|
||||||
|
die("Post not found");
|
||||||
|
}
|
||||||
|
|
||||||
|
$title = $row["title"];
|
||||||
|
$content = $row["content"];
|
||||||
|
$date = $row["date"];
|
||||||
|
$id = $row["id"];
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode([
|
||||||
|
"title" => $title,
|
||||||
|
"content" => $content,
|
||||||
|
"date" => $date,
|
||||||
|
"id" => $id
|
||||||
|
]);
|
|
@ -1,32 +1,15 @@
|
||||||
<?php
|
|
||||||
include "internal/header.php";
|
|
||||||
include "internal/footer.php";
|
|
||||||
|
|
||||||
if(isset($_GET["template"])){
|
<!DOCTYPE html >
|
||||||
$title = "[TITLE]";
|
<html lang = "de" >
|
||||||
$content = "[CONTENT]";
|
<html >
|
||||||
$date = "DATE NOT SHOWN IN PREVIEW";
|
<head >
|
||||||
$id = "0";
|
<meta charset = "UTF-8" >
|
||||||
|
<meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
|
||||||
|
<title></title >
|
||||||
|
|
||||||
} else{
|
<link href = "/css/style.css" rel = "stylesheet" >
|
||||||
$id = $conn->real_escape_string($_GET["id"]);
|
</head >
|
||||||
$result = $conn->query("SELECT * FROM posts WHERE id=$id");
|
<body >
|
||||||
if ($result->num_rows > 0) {
|
<script src="/js/script.js"></script>
|
||||||
$row = $result->fetch_assoc();
|
</body>
|
||||||
} else {
|
</html>
|
||||||
die("Post not found");
|
|
||||||
}
|
|
||||||
|
|
||||||
$title = $row["title"];
|
|
||||||
$content = $row["content"];
|
|
||||||
$date = $row["date"];
|
|
||||||
$id = $row["id"];
|
|
||||||
}
|
|
||||||
|
|
||||||
getHeader($title, "");
|
|
||||||
?>
|
|
||||||
<p>
|
|
||||||
<?php echo($content); ?>
|
|
||||||
</p>
|
|
||||||
<?php
|
|
||||||
getFooter();
|
|
Loading…
Reference in a new issue