dongshi9407 2015-02-20 12:19
浏览 5
已采纳

查询选择类似当前的类似帖子

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?

  • 写回答

1条回答 默认 最新

  • dsc7188 2015-02-20 12:25
    关注

    Just try with that query.

    "SELECT * FROM posts INNER JOIN category ON posts.category=category.cat_id WHERE posts.category = '".$row['category']."' LIMIT 3"

    Be sure that you have only one element in $query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度