title){
printf("
Article not found
I am sorry, but I couldn't find an article with this ID.
");
return;
}
// Begin the article
printf("");
// Display the blogpost name
printf("%s
", $this->title);
// Display the blog metadata
printf("");
// Display tags
for($i = 0; $i < count($this->tags); $i++) {
$tag = $this->tags[$i];
printf("
%s
", $tag["color"], $tag["name"]);
}
// Display publish date and end metadata div
printf("Published on: %s
",
date("Y-m-d", strtotime($this->date_posted)));
// Display hrule, article content and end the article
printf("
%s", $this->content);
}
/**
* Constructor for the blogpost.
* $blogpost_id GUID of the blogpost in the database.
* $title Title of the blogpost.
* $content Content of the blogpost article.
* $date_posted Timestamp at publishing of article.
* $tags Array of the tags this article has.
*/
public function __construct($blogpost_id, $title, $content,
$date_posted, $tags){
$this->blogpost_id = $blogpost_id;
$this->title = $title;
$this->content = $content;
$this->date_posted = $date_posted;
$this->tags = $tags;
}
}
/**
* Try to load info about the blog with guid in GET and set global
* variables accordingly.
*/
function get_blog_info($conn, $blogId){
// Check DB connection
if($conn == null){
printf("
Failed DB connection, cannot proceed!
If you see this error in production,
please shoot me an email with helpful details.
");
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 null;
}
// 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;
return new Blogpost($blogId, $blogTitle, $blogContent, $datePosted, $tags);
}
/**
* Display all the comments responding to a given article.
*/
function display_comments($conn, $blogId){
// Check DB connection
if($conn == null){
printf("
Failed to load comments due to database connection error!
If you see this error in production,
please shoot me an email with helpful details.
");
return;
}
// Prepare statement for selecting all coments replying to a given article.
$stmt = $conn->prepare("SELECT;");
// Bind and execute the tag select
$stmt->bindParam(":blogpost_id", $blogId);
$stmt->execute();
// Fetch the tags
$tags_arr = $stmt->fetchall(PDO::FETCH_ASSOC);
}
// Get the blog id.
$blogId = sanitize_input($_GET["guid"]);
$blogPost = null;
// Try to get info about blog
if($blogId) {
$blogPost = get_blog_info($conn, $blogId);
}
// Display the header with title being the blog name, or not found message.
display_header($blogPost ? $blogPost->title : "Article not found");
// Display the blog
$blogPost->display_article();
include_once($COMMONS."/footer.php");
?>