dynamic blogs and delayed header display

This commit is contained in:
Zdenek Borovec 2024-04-28 15:21:09 +02:00
parent 3c65fe1699
commit a1075b0d5e
8 changed files with 265 additions and 85 deletions

View file

@ -31,15 +31,20 @@ Anyways, let's get to the *how*.
- The basic skeleton for any document should be: - The basic skeleton for any document should be:
```php ```php
<?php <?php
// Title for your page
$PageTitle="Your title";
// This makes for less typing while including the header and the footer. // This makes for less typing while including the header and the footer.
// Absolute path to the commons folder. // Absolute path to the commons folder.
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common"; $COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
// Include the common site-wide header. // Include the common site-wide header.
include_once($COMMONS."/header.php"); include_once($COMMONS."/header.php");
// Display the header with desired title. This is a separate function for the
// cases where you want to set the title dynamically after doing some
// processing (which requires sql connection from the header for example).
// An example of this are the blogs, which first get their name from the
// databese, and only then display the header with the title being the name
// of the article.
display_header(<PAGETITLE>);
?> ?>
<!-- <!--

View file

@ -7,7 +7,8 @@ include_once("config.php");
session_start(); session_start();
// PDO Mysql connection object // PDO Mysql connection object
$conn = ""; $conn = null;
attempt_sql_connect();
/** Attempt to connect to sql database /** Attempt to connect to sql database
*/ */
@ -33,26 +34,49 @@ function attempt_sql_connect() {
} }
} }
attempt_sql_connect(); /**
* Start the html document, set headers, begin body and display the default
?> * navbar.
* <html><body> nad <main> tags will be left open, to close them
<html lang="en"> * in the default way include footer.php
* This should be the first function to write any text outside of debugging
* purposes.
*/
function display_header($pageTitle) {
printf("
<html lang=\"en\">
<head> <head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">
<link rel="icon" href="http://www.zdenekborovec-dev.cz/favicon.ico"> <link rel=\"icon\" href=\"http://www.zdenekborovec-dev.cz/favicon.ico\">
<link rel="stylesheet" href="http://assets.zdenekborovec-dev.cz/common/styles.css?1703173954"> <link rel=\"stylesheet\"
<title><?= isset($PageTitle) ? $PageTitle : "Zdenek Home Page"?></title> href=\"http://assets.zdenekborovec-dev.cz/common/styles.css?TIMESTAMP\">
<title>%s</title>
</head> </head>
<body> <body>
<main> <main>
<div id="header"> <div id=\"header\">
<h1> Zdenek Borovec Home Page </h1> <h1> Zdenek Borovec Home Page </h1>
<ul> <ul>
<li><a href="http://www.zdenekborovec-dev.cz">About</a></li> <li>
<li><a href="http://www.zdenekborovec-dev.cz/blog">Blog</a></li> <a href=\"http://www.zdenekborovec-dev.cz\">
<li>Gallery</li> About
<li><a href="http://www.zdenekborovec-dev.cz/random">Random Tools</a></li> </a>
</li>
<li>
<a href=\"http://www.zdenekborovec-dev.cz/blog\">
Blog
</a>
</li>
<li>
Gallery
</li>
<li>
<a href=\"http://www.zdenekborovec-dev.cz/random\">
Random Tools
</a>
</li>
</ul> </ul>
</div> </div>", $pageTitle);
}
?>

120
docs/www/blog/article.php Normal file
View file

@ -0,0 +1,120 @@
<?php
// Declare global variables for the blog content
$blogTitle = $blogContent = $datePosted = $tags = "";
$blogId = $_GET["guid"];
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
include_once($COMMONS."/header.php");
// Try to get info about blog
if($blogId)
get_blog_info();
// Display the header with title being the blog name, or not found message.
display_header($blogTitle ? $blogTitle : "Article not found");
/**
* Try to load info about the blog with guid in GET and set global
* variables accordingly.
*/
function get_blog_info(){
// Access global variables
global $conn;
global $blogTitle;
global $blogContent;
global $datePosted;
global $tags;
global $blogId;
// Check DB connection
if($conn == null){
printf("
<article>
<h1>Failed DB connection, cannot proceed!</h1>
If you see this error in production,
please shoot me an email with helpful details.
</article>");
include_once($COMMONS."/footer.php");
die();
}
// Prepare and bind statement for gathering blogpost info
$stmt = $conn->prepare("SELECT title, content, date_posted
FROM blogposts WHERE blogpost_id = :blogpost_id;");
$stmt->bindParam(":blogpost_id", $blogId);
// Execute the statement
$stmt->execute();
// Fetch the blogpost
$result = $stmt->fetch(PDO::FETCH_ASSOC);
// If no post with given guid was found,
// there is no information to gather, return.
if(!$result){
return;
}
// 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;");
// Bind and execute the tag select
$stmt->bindParam(":blogpost_id", $blogId);
$stmt->execute();
// Fetch the tags
$tags_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
// Set the variables
$blogTitle = $result["title"];
$blogContent = $result["content"];
$datePosted = $result["date_posted"];
$tags = $tags_arr;
}
// If a blog with given ID was not found display warning message and die.
if(!$blogTitle){
printf("
<article>
<h2> Article not found </h2>
<hr>
<p>
I am sorry, but I couldn't find an article with this ID.
</p>
</article>
");
include_once($COMMONS."/footer.php");
die();
}
// Begin the article
printf("<article>");
// Display the blogpost name
printf("<h2>%s</h2>", $blogTitle);
// Display the blog metadata
printf("<div class=\"blog-metadata\">");
// Display tags
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"]);
}
// Display publish date and end metadata div
printf("<span class=\"blog-publish-date\">Published on: %s</span></div>",
date("Y-m-d", strtotime($datePosted)));
// Display hrule, article content and end the article
printf("</article><hr><article>%s</article>", $blogContent);
include_once($COMMONS."/footer.php");
?>

View file

@ -1,9 +1,96 @@
<?php <?php
$PageTitle="Zdenek: Blog";
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common"; $COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
include_once($COMMONS."/header.php"); include_once($COMMONS."/header.php");
display_header("Blogs");
/**
* 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.
*/
function display_blog_previews(){
// Access global variables
global $conn;
// Check DB connection
if($conn == null){
printf("
<article>
<h1>Failed DB connection, cannot proceed!</h1>
If you see this error in production,
please shoot me an email with helpful details.
</article>");
return;
}
// 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);
}
}
?> ?>
<article> <article>
@ -11,65 +98,14 @@ include_once($COMMONS."/header.php");
My blogposts My blogposts
</h2> </h2>
<p> <p>
Blogs about various topics will be written here, at some point in the future. These can range from well thought out slow-burning blood-churning gems
</p> to random brain farts in textual form, you can use tags to filter.
<p>
The way in which they are made and addressed right now is temporary, so any links you save / share WILL EXPIRE eventualy. This includes media.
</p>
<p>
I am just trying to get a feel for what tools I might need for writing the blogs, so everything is subject to change once I get a solid idea and implement a proper system for doing this, instead of just writing plain html.
</p> </p>
</article> </article>
<hr> <hr>
<ul class="blog-preview-list">
<li><div class="blog-preview"><a href="http://www.zdenekborovec-dev.cz/blog/flatland_review">
<h3>
Flatland: A Review.
</h3>
</a>
<div class="blog-metadata">
<span class="blog-tag" style="background-color: AntiqueWhite">
books
</span>
<span class="blog-tag" style="background-color: LightSkyBlue">
geometry
</span>
<span class="blog-tag" style="background-color: LightPink">
reviews
</span>
<span class="blog-publish-date">
Published on: 2024-03-12
</span>
</div>
<p>
I had the privilege to travel Flatland together with A. Square these past few days and the experience was truly delightful.
</p>
</div></li>
<li><div class="blog-preview"><a href="http://www.zdenekborovec-dev.cz/blog/godot_spell_system_prototype">
<h3>
Godot spell casting system prototype
</h3>
</a>
<div class="blog-metadata">
<span class="blog-tag" style="background-color: cornflowerblue">
godot
</span>
<span class="blog-tag" style="background-color: khaki">
game-dev
</span>
<span class="blog-tag" style="background-color: sienna">
the-labyrinth-II
</span>
<span class="blog-publish-date">
Published on: 2024-01-13
</span>
</div>
<p>
I have wanted to implement this system in my upcoming game "The Labyrith II" for a few years now, but I got distracted and left it, well, I am back now.
</p>
</div></li>
</ul>
<?php <?php
display_blog_previews();
include_once($COMMONS."/footer.php"); include_once($COMMONS."/footer.php");
?> ?>

View file

@ -1,9 +1,8 @@
<?php <?php
$PageTitle="Zdenek: About";
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common"; $COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
include_once($COMMONS."/header.php"); include_once($COMMONS."/header.php");
display_header("About");
?> ?>
<article> <article>

View file

@ -1,10 +1,8 @@
<?php <?php
$PageTitle="Login";
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common"; $COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
include_once($COMMONS."/header.php"); include_once($COMMONS."/header.php");
display_header("Login");
// Define previous attempt and error variables and set to empty values. // Define previous attempt and error variables and set to empty values.
$emailOld = $passwordOld = ""; $emailOld = $passwordOld = "";

View file

@ -1,9 +1,8 @@
<?php <?php
$PageTitle="Unit conversions";
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common"; $COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
include_once($COMMONS."/header.php"); include_once($COMMONS."/header.php");
display_header("Unit conversions");
?> ?>
<article id="toc"> <article id="toc">

View file

@ -1,9 +1,8 @@
<?php <?php
$PageTitle="Zdenek: Random tools";
$COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common"; $COMMONS = $_SERVER['DOCUMENT_ROOT'] . "/../common";
include_once($COMMONS."/header.php"); include_once($COMMONS."/header.php");
display_header("Random tools");
?> ?>
<article> <article>