personal-website/docs/www/blog/index.php

106 lines
2.4 KiB
PHP
Raw Normal View History

2023-12-21 13:11:22 +01:00
<?php
2024-05-10 17:20:25 +02:00
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
include_once($COMMONS."/header.php");
/**
* Display a blog-preview div
*/
function display_blog_preview($blogpost_id, $title, $abstract,
$date_posted, $tags){
printf("
<div class=\"blog-preview\">
<a href=\"http://www.zdenekborovec-dev.cz/blog/article?guid=%s\">
<h3>
%s
</h3>
</a>
<div class=\"blog-metadata\">
", $blogpost_id, $title);
for($i = 0; $i < count($tags); $i++) {
$tag = $tags[$i];
printf("
<span class=\"blog-tag\" style=\"background-color: %s\">
%s
</span>", $tag["color"], $tag["name"]);
}
printf("
<span class=\"blog-publish-date\">
Published on: %s
</span>
</div>
<p>
%s
</p>
</div>
", date("Y-m-d", strtotime($date_posted)), $abstract);
}
/**
* Select all the blogposts from the database, their tags, then display them.
*/
2024-05-08 16:00:44 +02:00
function display_blog_previews($conn){
// Check DB connection
if($conn == null){
2024-05-10 17:20:25 +02:00
header($_SERVER["SERVER_PROTOCOL"]." 503 Service Unavailable", true, 503);
include_once($_SERVER["DOCUMENT_ROOT"]."/errors/503.php");
include_once($COMMONS."/footer.php");
2024-05-08 16:00:44 +02:00
die();
}
// Prepare statement for selecting all the blogposts
$stmt = $conn->prepare("SELECT blogpost_id, title, abstract, date_posted
FROM blogposts ORDER BY date_posted DESC;");
// Execute the statement
$stmt->execute();
// Fetch the blogposts
$blogposts_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
// Prepare new statement for selecting the tags for a given blogpost
$stmt = $conn->prepare("SELECT name, color FROM
blogpost_tags INNER JOIN blogpost_has_tag ON
blogpost_tags.tag_id = blogpost_has_tag.tag_id WHERE
blogpost_id = :blogpost_id;");
// Go through all the blogposts, fetch their tags and display them
for($i = 0; $i < count($blogposts_arr); $i++) {
// Get info for the current blog
$blog = $blogposts_arr[$i];
// Bind and execute the tag select
$stmt->bindParam(":blogpost_id", $blog["blogpost_id"]);
$stmt->execute();
// Fetch the tags
$tags_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
// Display the blog
display_blog_preview($blog["blogpost_id"], $blog["title"],
$blog["abstract"], $blog["date_posted"], $tags_arr);
}
}
2024-05-08 16:00:44 +02:00
display_header("Blogs");
2023-12-21 13:11:22 +01:00
?>
<article>
2024-01-19 04:17:10 +01:00
<h2>
My blogposts
</h2>
<p>
These can range from well thought out slow-burning blood-churning gems
to random brain farts in textual form, you can use tags to filter.
2024-01-19 04:17:10 +01:00
</p>
2023-12-21 13:11:22 +01:00
</article>
2024-01-19 04:17:10 +01:00
<hr>
2023-12-21 13:11:22 +01:00
<?php
2024-05-08 16:00:44 +02:00
display_blog_previews($conn);
2024-05-10 17:20:25 +02:00
include_once($COMMONS."/footer.php");
2023-12-21 13:11:22 +01:00
?>