Enhancing Blog Functionality and Post Organization
Last modified: May 18 2024 18:40:36
This blog post covers recent updates to the blog project, focusing on improving error handling, post visibility, and metadata display. These changes enhance the overall user experience and provide better management of blog posts.
Introduction
Enhancing the functionality and organization of a blog project is essential for providing a seamless user experience. In this update, we have made several improvements to the index.php
and search.php
files, as well as renamed post files for better structure. Let's explore these changes in detail.
Updates in index.php
The index.php
file has been updated to improve error handling, manage hidden posts more effectively, and display additional metadata for each post. Here are the key changes:
diff --git a/index.php b/index.php
index d62e1dd..4f3352d 100644
--- a/index.php
+++ b/index.php
@@ -15,26 +15,45 @@
<h2>Blog Posts</h2>
<ul class="post-list">
<?php
+ // Turn off error reporting
+ error_reporting(0);
+
+
// Read hidden posts from hidden.txt
- $hiddenPosts = file('hidden.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ $hiddenPosts = array();
+ if (file_exists("hidden.txt")) {
+ $hiddenPosts = file("hidden.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ }
+ // Read post data
+ $postData = array();
$posts = array_diff(scandir('posts'), array('.', '..'));
- if (count($posts) > 0) {
- foreach ($posts as $post) {
- $postName = pathinfo($post, PATHINFO_FILENAME);
- $postNameDisplay = str_replace('_', ' ', $postName);
- $postPath = 'posts/' . $post;
-
- // Skip if the post is in the hidden list
- if (in_array($post, $hiddenPosts)) {
- continue;
- }
- $lastModified = date("F d Y H:i:s", filemtime($postPath));
- echo '<li><a href="' . htmlspecialchars($postPath) . '">' . htmlspecialchars($postNameDisplay) . '</a><span class="date">Last modified: ' . htmlspecialchars($lastModified) . '</span></li>';
+ foreach ($posts as $post) {
+
+ // Skip if the post is in the hidden list
+ if (in_array($post, $hiddenPosts)) {
+ continue;
}
- } else {
- echo '<li>No posts available.</li>';
+
+ $postPath = 'posts/' . basename($post);
+ $postData[$post] = filemtime($postPath);
+ }
+
+ foreach ($postData as $post => $modifiedTime) {
+ $postName = pathinfo($post, PATHINFO_FILENAME);
+ $postParts = explode('-', $postName);
+ $projectName = $postParts[0];
+ $postNumber = $postParts[1];
+ $postNameDisplay = str_replace('_', ' ', $postName);
+ $postNameDisplay = explode('-', $postNameDisplay)[2];
+ $postPath = 'posts/' . basename($post);
+
+ $lastModified = date("F d Y H:i:s", $modifiedTime);
+ echo '<li><a href="' . htmlspecialchars($postPath) . '">' . htmlspecialchars($postNameDisplay) . '</a>';
+ echo '<span class="project">Project: ' . htmlspecialchars($projectName) . ' |</span>';
+ echo '<span class="post-number">| Post #' . htmlspecialchars($postNumber) . '</span>';
+ echo '<span class="date">Last modified: ' . htmlspecialchars($lastModified) . '</span></li>';
}
?>
</ul>
These updates include:
- Turning off error reporting for a cleaner user experience.
- Checking if
hidden.txt
exists before reading it, preventing potential errors. - Enhanced post data handling by storing modification times in an array for better management.
- Displaying additional metadata such as project name and post number, parsed from the post filename.
Post File Renaming
To improve the organization and readability of post files, they have been renamed to include project names and post numbers:
diff --git a/posts/Creating_the_Blog_Project.php b/posts/Blog-1-Creating_the_Blog_Project.php
similarity index 100%
rename from posts/Creating_the_Blog_Project.php
rename to posts/Blog-1-Creating_the_Blog_Project.php
diff --git a/posts/Enhancing_CSS_Styles.php b/posts/Blog-2-Enhancing_CSS_Styles.php
similarity index 100%
rename from posts/Enhancing_CSS_Styles.php
rename to posts/Blog-2-Enhancing_CSS_Styles.php
This renaming scheme ensures each post is easily identifiable by its project and sequence number, enhancing file organization.
Updates in search.php
Similar to index.php
, the search.php
file received updates to improve error handling and metadata display during search operations. Here are the changes:
diff --git a/search.php b/search.php
index 3395cae..5398b08 100644
--- a/search.php
+++ b/search.php
@@ -10,11 +10,17 @@
<h1>Search Results</h1>
<ul class="post-list">
<?php
+ // Turn off error reporting
+ error_reporting(0);
+
if (isset($_GET['query'])) {
$query = htmlspecialchars($_GET['query']);
// Read hidden posts from hidden.txt
- $hiddenPosts = file('hidden.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ $hiddenPosts = array();
+ if (file_exists("hidden.txt")) {
+ $hiddenPosts = file("hidden.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+ }
$posts = array_diff(scandir('posts'), array('.', '..'));
$found = false;
@@ -23,13 +29,20 @@
if (in_array($post, $hiddenPosts)) {
continue;
}
-
+
if (stripos($post, $query) !== false) {
$postName = pathinfo($post, PATHINFO_FILENAME);
+ $postParts = explode('-', $postName);
+ $projectName = $postParts[0];
+ $postNumber = $postParts[1];
$postNameDisplay = str_replace('_', ' ', $postName);
- $postPath = 'posts/' . $post;
+ $postNameDisplay = explode('-', $postNameDisplay)[2];
+ $postPath = 'posts/' . basename($post);
$lastModified = date("F d Y H:i:s", filemtime($postPath));
- echo '<li><a href="' . htmlspecialchars($postPath) . '">' . htmlspecialchars($postNameDisplay) . '</a><span class="date">Last modified: ' . htmlspecialchars($lastModified) . '</span></li>';
+ echo '<li><a href="' . htmlspecialchars($postPath) . '">' . htmlspecialchars($postNameDisplay) . '</a>';
+ echo '<span class="project">Project: ' . htmlspecialchars($projectName) . ' |</span>';
+ echo '<span class="post-number">| Post #' . htmlspecialchars($postNumber) . '</span>';
+ echo '<span class="date">Last modified: ' . htmlspecialchars($lastModified) . '</span></li>';
$found = true;
}
}
Key improvements in search.php
include:
- Turning off error reporting for a cleaner search experience.
- Checking if
hidden.txt
exists before reading it, preventing potential errors. - Displaying additional metadata such as project name and post number during search results.
Conclusion
These updates significantly improve the functionality and organization of our blog project. By enhancing error handling, managing hidden posts more effectively, and displaying additional metadata, we provide a better user experience and streamline post management. Future updates will continue to build on these improvements.