I have table posts
with following rows:
post_id
post_title
post_image
post_body
category
Second table category
where categories are stored
cat_id
cat_name
When I click on post 1 it is open in new page post.php?post_id=1
.. What I trying to do now is on the new page to have similar post only from the same category.
This is the code that I have on single post page
function truncate–s($text, $chars = 80) {
$text = $text." ";
$text = substr($text,0,$chars);
$text = substr($text,0,strrpos($text,' '));
$text = $text." ...";
return $text;
}
if(isset($_GET['post_id']) && is_numeric($_GET['post_id'])){
$post_id = $_GET['post_id'];
$query = $pdo->query("SELECT * FROM posts WHERE post_id = $post_id");
foreach ($query as $row) {
echo '<div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
<div class="portfolio-slider-wrapper">
<ul id="portfolio-slider">
<h4>'.$row['post_title'].'</h4>
<hr>
<li><img src="'.$row['post_image'].'" alt="" class="img-responsive"/> </li>
</ul>
</div>
<div class="blog-desc">
<ul class="post-meta-links list-inline">
<li><a href="#"><span> <i class="fa fa-calendar"></i></span>'.$row['post_date'].'</a></li>
</ul>
<p>
'.$row['post_body'].'
</p>
</div>
}
echo ' <div class="clearfix"></div>
<div class="related-post">
<h4>Related</h4>
<hr>';
$query1 = $pdo->query("SELECT * FROM posts INNER JOIN category ON posts.category=category.cat_id LIMIT 3");
foreach ($query1 as $post) {
echo '<div class="col-md-4 col-sm-4">
<div class="rel-post">
<a href="#">
<img src="'.$post['post_image'].'" alt="" lass="img-responsive">
<div class="caption">
<h4>'.$post['post_title'].'</h4>
<p>'.truncate–s($post['post_body']).'</p>
</div>
</a>
</div>
</div>';
}
Currently when I open some post it doesn't mater what ID have it's always load first 3 post from the database table.
How to load the posts from same category?