In this tutorial, we are going to display the recommended blog posts using PHP and MYSQL database . To display the recommended blog posts, you have to use the PHP operator. Here we are creating a complete blog from the first tutorial "how to create a blog from scratch in PHP". In this tutorial, we will display the recommended blog post. Displaying the next 5 or 6 posts is called the recommended blog post.We will create an MYSQL query in such a way that the next 5 or 10 rows can be fetched on the same page. Displaying next rows data on a current open blog post is called a recommended post.
Next 5 or 10 blog posts can be easily displayed by PHP and MYSQL. Along with displaying the next data, the data limit can also be set.
Fetch recommended Blog post from MYSQL database table –
To fetch the recommended blog post, an ID of the current blog post is required, due to which the next data can be fetched from the database. Like
Suppose – blog post id is 2
Let's understand the MYSQL query –
SELECT * from table_name where articleId>2 order by articleId ASC ;
In the MYSQL query above, we are using current blog post id to display the next recommended row data. We are using > PHP (greater than) operator to fetch the next rows data.
How to set the limit for next rows data in PHP?
We can set a limit of rows in PHP. The limit is another best way to create a boundary for rows data. Let’s create an MYSQL query for next rows data.
SELECT * from table_name where articleId>2 order by articleId ASC limit 5;
In the above, we are used to limiting 5. We can create limit 1, 2, 3,4,5,6….n
SELECT * from table_name where articleId>2 order by articleId ASC limit 6;
SELECT * from table_name where articleId>2 order by articleId ASC limit 10;
Now, back to the PHP blog CMS and add the code below after the social share code.
blog/show.php
Recomended Posts:
<?php
// run query//select by current id and display the next 5 blog posts
$recom= $db->query("SELECT * from techno_blog where articleId>$articleIdc order by articleId ASC limit 5");
// look through query
while($row1 = $recom->fetch()){
echo '';
}
?>
In the code above, you can see that we are displaying the next 5 blog posts on the same page. Here we used the blog title and slug using the id of the current open blog post by fetching the next 5 rows data. Here we have set the limit as 5. The limit can be further increased to display more recommended posts.
Recommended Posts:-