简体   繁体   English

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

[英]How to filter multiple categories and show it's posts/articles PHP

I have created a page that shows all the posts that are under a single category ie if i click on the category Music i will get all the Articles that are connected with Music category.我创建了一个页面,该页面显示单个类别下的所有帖子,即如果我点击类别音乐,我将获得与音乐类别相关的所有文章。

But my goal is to create a filtering option that can filter out certain categories and only show all the posts that are connected to the categories you have filtered ie i'll have a bunch of categories with checkboxes behind it and if i check Music and Games and submit the form i want to see all posts under music and games.但我的目标是创建一个过滤选项,可以过滤掉某些类别,只显示与您过滤的类别相关的所有帖子,即我将有一堆带有复选框的类别,如果我选中音乐和游戏并提交表格我想查看音乐和游戏下的所有帖子。

Here's the code i'm using to show all posts under 1 single category.这是我用来显示 1 个单一类别下的所有帖子的代码。

<?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>

So i started out with the form but it's not quite good since it only sends out the category names and not the corresponding category id.所以我从表单开始,但它不是很好,因为它只发送类别名称而不是相应的类别 id。

<form action="c.php" method="get">
  <fieldset>
    <legend>Filter Categories</legend>
    <p>
      <label><input type="checkbox" name="cat[]" value="Music"/> Music</label>
      <label><input type="checkbox" name="cat[]" value="Games"/> Games</label>
      <label><input type="checkbox" name="cat[]" value="Tech"/> Tech</label>
    </p>
  </fieldset>
  <button type="submit">Filter</button>
  <button type="reset">Reset</button>
</form>

Here´s my database structure:这是我的数据库结构:

blog_posts table blog_posts

postID int, primary, auto increment. postID int,主要,自动增量。

postTitle varchar后标题varchar

postCont text

blog_categories table blog_categories

catID int, primary, auto increment. catID int,主要,自动增量。

catName varchar猫名varchar

blog_posts_categories table blog_posts_categories

postID int postID整数

catID int猫 ID整数


Database content:数据库内容:

blog_posts blog_posts

postID  |  postTitle  | postCont

  1          Post1       Cont1
  2          Post2       Cont2
  3          Post3       Cont3
  4          Post4       Cont4
  5          Post5       Cont5

blog_categories blog_categories

catID   |   catName

  1          Music
  2          Games
  3        Technology

blog_posts_categories blog_posts_categories

postID  |  catID

  1          1
  1          2
  1          3
  2          2
  3          3
  4          1
  4          2
  5          2
  5          3

The form content表格内容

Replace your checkboxes values with the actual category id, and put the category name in the tag inner text content:用实际的类别 id 替换您的复选框值,并将类别名称放在标签内部文本内容中:

<input type="checkbox" name="cat[]" value="<?php echo $row['catID'];?>"/><?php echo $row['catName'];?></input>

You can get that data out of the DB (the blog_categories table) and do a simple loop on the rows of the result set.您可以从数据库( blog_categories表)中获取该数据,并对结果集的行进行简单的循环。

The table display表显示

Now you should get an array in $_GET["cat"] of all the selected category ids;现在您应该在$_GET["cat"]获得所有选定类别 ID 的数组; you just need to implode it with comma as a separator, and put it in your query, replacing WHERE catID = $catID" with WHERE catID IN ($catID)" .您只需要使用逗号作为分隔符将其内爆,并将其放入您的查询中,将WHERE catID = $catID"替换为WHERE catID IN ($catID)" Don't forget to sanitize your request data first (by checking that the values are integers).不要忘记首先清理您的请求数据(通过检查值是否为整数)。

Lastly, you might want to update your table display by adding the category the post belongs to in an extra column, or sort your data by category, and have sub tables for each category.最后,您可能希望通过在额外的列中添加帖子所属的类别来更新表格显示,或者按类别对数据进行排序,并为每个类别设置子表。 This is left as an exercise.这留作练习。

   <?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>

I have same problem, I like to get a specific category that will post in specific page.我有同样的问题,我喜欢获得将在特定页面上发布的特定类别。 I have this php code to fetch information from my database;我有这个 php 代码可以从我的数据库中获取信息;

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

I have 3 categories: cat1 / cat2 / cat3.我有 3 个类别:cat1 / cat2 / cat3。 I like all cat1 category filtered and will post only in one page我喜欢过滤所有的 cat1 类别,并且只会在一页中发布

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM