Section 3 displays a preview of the latest blog post, including the date, thumbnail, and excerpt:

In order to render the latest blog entry, we first must query the database like so:
<?php
//Connect Database include("connect.php")
try{
$db = new PDO("mysql:host=localhost;dbname=john_doe_db;charset=utf8",
"john_doe_blog",
"password123457x");
} catch(PDOException $e){
//if connections fails
die("No Connection!");
}
//SQL statement to query the latest blog post from the POSTS TABLE
$sql="SELECT postID, postDate, postTitle, postBild, postBildAlt, postMedia, postTube, postBody
FROM posts LIMIT 1";
//prepared statement. Optional in this case due to no wildcard and no $_GET
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$abfrage=$db->prepare($sql);
$abfrage->execute();
//local german timezone.
setlocale(LC_TIME, "de_DE.utf8");
//fetch
$row = $abfrage->fetch();
?>
Next we first convert from English to German time format (IT’s a german blog) and then calculate the average post rating with another SQL statement. we use an if statement in order to distinguish between two cases. case 1 the post hasn’t been rated yet. case 2 the post already has been rated.
<?php
//Timestamp is saved as strin in the db therefor we neeed the strtotime() method
$strtotime = strtotime($row["postDate"]);
//German weekday
$strftimeA = strftime("%A", $strtotime);
//Day of the month
$strftimee = strftime("%e", $strtotime);
//Month no abbr.
$strftimeB = strftime("%B", $strtotime);
//Year
$strftimeY = strftime("%Y", $strtotime);
//Hours
$strftimeH = strftime("%H", $strtotime);
//Minutes
$strftimeM = strftime("%M", $strtotime);
//trim text and strip tags...
$dnotags = strip_tags($row["postBody"]);
$b200 = substr($dnotags, 0, 200);
$bstrr = strrpos($b200, " ");
$btrim = substr($b200, 0, $bstrr);
//Calculate average rating form the RATING TABLE using sql
$sql2="SELECT AVG(rateRating) AS durchschnitt
FROM pratings
WHERE postFID = :id
";
//We use the id from the POSTS TABLE
$postID = $row["postID"];
$abfrage2=$db->prepare($sql2);
$abfrage2->bindParam(":id",$postID);
$abfrage2->execute();
$row2 = $abfrage2->fetch();
$avg = number_format($row2["durchschnitt"], 1, ",", ".");
//
if($row2["durchschnitt"] == null){
$durchschnitt = "<p class='ml-2'>noch nicht bewertet";
} else {
$durchschnitt = "<p class='ml-2'><span class='text-danger'> $avg</span>/5 Punkten <br>";
}
?>
Let’s now count how many time the post was rated
<?php
$sql3="SELECT COUNT(rateRating) AS anzahl
FROM pratings
WHERE postFID = :id
";
$abfrage3=$db->prepare($sql3);
$abfrage3->bindParam(":id",$postID);
$abfrage3->execute();
$row3 = $abfrage3->fetch();
$n =$row3["anzahl"];
if($n == null){
$anzahl = "</p>";
}else {
$anzahl = "Bewertet von <span class='text-danger'> $n </span> Besucher</p>";
}
?>
Finally let’s render our latest post and produce some HTML.
<!-- Render latest post-->
<div class="card bg-dark text-light mx-5 mt-1 mb-5">
<div class="card-body ppost">
<div class="pimg mt-2 mx-2 rounded">
<img class="rounded-top" src="bilder/<?php echo $row["postBild"];?>" alt="<?php echo $row["postBildAlt"]?>" style="max-width:100%;">
<?php echo $durchschnitt?>
<?php echo $anzahl?>
</div>
<div class="ml-2 ">
<h4 class="mt-0 pt-0"><?php echo $row["postTitle"];?></h4>
<span class="text-danger">gepostet am: <?php echo $strftimeA;?> den <?php echo $strftimee;?>. <?php echo $strftimeB;?> <?php echo $strftimeY;?> um <?php echo $strftimeH;?>:<?php echo $strftimeM;?> Uhr.</span>
<p class="text-justify"><?php echo $btrim ?>...<a href="blog.php?blogpost=<?php echo $row["postID"]?>"> Weiterlesen</a></p>
</div>
</div>
</div>
Done!