I have a blog site in PHP and MySQL with tow table, POST and CATEGORIES. I need help, how to display related post from same category when visitor clicked on a single post title. your help will be greatly appreciated
POST table
POST_ID | CAT_ID | TITLE | DESCRIPTION | IMAGE |
---|---|---|---|---|
1 | 3 | |||
2 | 5 | |||
3 | 1 | |||
4 | 6 |
CATEGORIES table
Cat_id | Cat_name | Total_post |
---|---|---|
3 | National | 5 |
5 | International | 7 |
1 | Sports | 3 |
6 | Technology | 2 |
Here is my Code:
<?php
if (isset($_GET['POST_ID'])) {
$POST_ID = $_GET['POST_ID'];
}
$sql1 = "select * from POST where POST_ID=$POST_ID";
$result1 = mysqli_query($conn, $sql1);
if (mysqli_num_rows($result1) > 0) {
?>
<div class="col-lg-4">
<div class="sidebar">
<div class="sidebar-widget">
<h2 class="sw-title">Related Posts</h2>
<div class="news-list">
<?php while ($row = mysqli_fetch_assoc($result1)) { ?>
<div class="nl-item">
<div class="nl-img">
<img src="<?php if (file_exists("upload/" . $row['image'])) {
echo "upload/" . $row['image'];
} else {
echo "upload/" . $row['image'];
} ?>" alt="" class="img-fluid">
</div>
<div class="nl-title">
<h4>
<a href="single-page.php?POST_ID=<?= $row['POST_ID']; ?>" title="<?= $row1['title']; ?>"><?= $row1['title']; ?></a>
</h4>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
</div>
<?php } ?>