Php 如何过滤多个类别并显示其帖子/文章

Php 如何过滤多个类别并显示其帖子/文章,php,mysql,filter,blogs,categories,Php,Mysql,Filter,Blogs,Categories,我已经创建了一个页面,显示单一类别下的所有帖子,即,如果我点击音乐类别,我将获得与音乐类别相关的所有文章 <?php require('includes/config.php'); $catID = ($_GET['id']); $catName = ($_GET ['cat']); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8">

我已经创建了一个页面,显示单一类别下的所有帖子,即,如果我点击音乐类别,我将获得与音乐类别相关的所有文章

<?php require('includes/config.php'); 

$catID = ($_GET['id']);
$catName = ($_GET ['cat']);



?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>XS</title>
    <link rel="stylesheet" href="style/normalize.css">
    <link rel="stylesheet" href="style/main.css">
</head>
<body>

    <div id="wrapper">

        <h1>XS <span style="color:blue;"> >> </span> <span><?php echo $catName); ?></span> </h1>

        <hr />

        <div class="grid">

        <?php
            try {
                $stmt = "SELECT * FROM blog_posts NATURAL JOIN blog_posts_categories WHERE catID = $catID";

                $query = $db->prepare($stmt);
                $query->execute();

                $numrows = $query->rowCount();
                if($numrows > 0){

                    while($row = $query->fetch()){
                        echo '<ol class="thumb-grid group">';
                            echo '<li>';
                                echo '<a href="viewpost.php?id='.$row['postID'].'">';
                                    echo '<img src="scripts/t.php?src='.$row['postImage'].'&w=236&h=236&q=95" alt="'.$row['postTitle'].'" title="'.$row['postTitle'].'" />';
                                echo '</a>';
                            echo '</li>';
                        echo '</ol>';
                    }

                }

            } catch(PDOException $e) {
                echo $e->getMessage();
            }
        ?>


        </div>

    </div>


</body>
</html>
但我的目标是创建一个过滤选项,它可以过滤掉某些类别,只显示与你过滤过的类别相关的所有帖子,也就是说,我会有一堆类别,后面有复选框,如果我选中音乐和游戏并提交表单,我希望看到音乐和游戏下的所有帖子

这是我用来显示一个类别下所有帖子的代码

<?php require('includes/config.php'); 

$catID = ($_GET['id']);
$catName = ($_GET ['cat']);



?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>XS</title>
    <link rel="stylesheet" href="style/normalize.css">
    <link rel="stylesheet" href="style/main.css">
</head>
<body>

    <div id="wrapper">

        <h1>XS <span style="color:blue;"> >> </span> <span><?php echo $catName); ?></span> </h1>

        <hr />

        <div class="grid">

        <?php
            try {
                $stmt = "SELECT * FROM blog_posts NATURAL JOIN blog_posts_categories WHERE catID = $catID";

                $query = $db->prepare($stmt);
                $query->execute();

                $numrows = $query->rowCount();
                if($numrows > 0){

                    while($row = $query->fetch()){
                        echo '<ol class="thumb-grid group">';
                            echo '<li>';
                                echo '<a href="viewpost.php?id='.$row['postID'].'">';
                                    echo '<img src="scripts/t.php?src='.$row['postImage'].'&w=236&h=236&q=95" alt="'.$row['postTitle'].'" title="'.$row['postTitle'].'" />';
                                echo '</a>';
                            echo '</li>';
                        echo '</ol>';
                    }

                }

            } catch(PDOException $e) {
                echo $e->getMessage();
            }
        ?>


        </div>

    </div>


</body>
</html>
博客分类

博客文章分类

形式内容 用实际类别id替换复选框值,并将类别名称放入标记内部文本内容中:

<input type="checkbox" name="cat[]" value="<?php echo $row['catID'];?>"/><?php echo $row['catName'];?></input>
您可以从blog_categories表的DB中获取数据,并对结果集的行执行简单的循环

表格显示 现在,您应该在$\u get[cat]中获得所有选定类别ID的数组;您只需使用逗号作为分隔符将其内爆,并将其放入查询中,将WHERE catID=$catID替换为WHERE catID in$catID。不要忘了首先通过检查值是否为整数来清理请求数据

最后,您可能希望通过在额外的列中添加帖子所属的类别来更新表显示,或者按类别对数据进行排序,并为每个类别创建子表。这只是一个练习

   <?php 
      $query = "SELECT * FROM `name` WHERE `status` = '1' ";
      if(isset($_GET['catname'])){
          $cat1 = $_GET['catname'];
        $query .= " AND `category` = '$cat1' ";
      }
      $query .= " ORDER BY `ptid` DESC ";

      $result = mysql_query($query);
      $nums = mysql_num_rows($result);
      if($nums > 0){
        while($rows = mysql_fetch_row($result,MYSQL_ASSOC)){
          $ptid_dec = $rows['ptid'];
          $mult = '987694929';
          $ptid = $ptid_dec*$mult;
          $productname = $rows['productname'];
          $productimage = $rows['productimage'];
          $description = $rows['description'];
   ?>

        <?php echo $productimage; ?>" 
        <div class="caption">
          <a href="admin/<?php echo $productimage; ?>" target="_blank"><h4 class="center listdesign"><strong><?php echo $productname; ?></strong></h4></a>


    <?php } } ?>

我有同样的问题,我喜欢得到一个特定的类别,将张贴在特定的页面。我有这个php代码从我的数据库中获取信息

<?php
        $result = mysqli_query ($mysqli, "SELECT * FROM mydata ORDER BY id DESC");
            while ($row = mysqli_fetch_array($result)) 
                                
{ 

我有三个类别:cat1/cat2/cat3。我喜欢过滤所有cat1类别,并且只在一个页面中发布

您正在呼应内爆的$links。你能编辑你的帖子并告诉我们什么是回显的,以及你试图运行的生成的查询是什么吗?我这样做只是为了测试,实际上从未删除它。代码现在只选择一个类别id,并显示连接到该类别的所有帖子。我想要实现的是过滤多个类别,以便显示所有连接到已过滤类别的帖子。如果您有catID列表,是什么阻止您运行以下操作:选择*from blog_posts NATURAL JOIN blog_posts_categories WHERE catID in.infrade,,$catIDs?使用上面的查询表这是如何回答问题的,如何过滤多个类别并显示其帖子/文章?
<input type="checkbox" name="cat[]" value="<?php echo $row['catID'];?>"/><?php echo $row['catName'];?></input>
   <?php 
      $query = "SELECT * FROM `name` WHERE `status` = '1' ";
      if(isset($_GET['catname'])){
          $cat1 = $_GET['catname'];
        $query .= " AND `category` = '$cat1' ";
      }
      $query .= " ORDER BY `ptid` DESC ";

      $result = mysql_query($query);
      $nums = mysql_num_rows($result);
      if($nums > 0){
        while($rows = mysql_fetch_row($result,MYSQL_ASSOC)){
          $ptid_dec = $rows['ptid'];
          $mult = '987694929';
          $ptid = $ptid_dec*$mult;
          $productname = $rows['productname'];
          $productimage = $rows['productimage'];
          $description = $rows['description'];
   ?>

        <?php echo $productimage; ?>" 
        <div class="caption">
          <a href="admin/<?php echo $productimage; ?>" target="_blank"><h4 class="center listdesign"><strong><?php echo $productname; ?></strong></h4></a>


    <?php } } ?>
<script>
$("#example1").DataTable({
  "pageLength": 9,
  "lengthChange": false,
  "bFilter": false,
  "bInfo": false,
  "bAutoWidth": false
});
$('#example2').DataTable({
  "paging": true,
  "lengthChange": false,
  "searching": false,
  "ordering": true,
  "info": true,
  "autoWidth": false
});
   <script>
 function paginateScroll() {
$('html, body').animate({
   scrollTop: $(".topheader").offset().top
}, 100);
console.log('pagination button clicked'); //remove after test
$(".paginate_button").unbind('click', paginateScroll);
$(".paginate_button").bind('click', paginateScroll);
}
paginateScroll();  
</script>
<?php
        $result = mysqli_query ($mysqli, "SELECT * FROM mydata ORDER BY id DESC");
            while ($row = mysqli_fetch_array($result)) 
                                
{