[英]How to get multiple category posts in category page?
I'm working on a custom CMS. 我正在开发自定义CMS。 My problem is I have a post with two categories 'tutorial' and 'language'.
我的问题是我有一个帖子,分为“教程”和“语言”两个类别。 I want to display it as an item in category page.
我想将其显示为类别页面中的项目。
This is my book_category table 这是我的book_category表
and this is my books table. 这是我的书桌。
Question 1: How to store book in database with multiple categories? 问题1:如何将图书存储在具有多个类别的数据库中? Should I store category ids as a string value in database?
我应该将类别ID作为字符串值存储在数据库中吗?
Question 2: How to query posts with multiple categories ? 问题2:如何查询多个类别的帖子?
This is my header.php code 这是我的header.php代码
<?php include 'includes/db.php'; ?>
<html lang="fa-IR" dir="rtl">
<head>
<title>CMS کتابخانه</title>
<script type="text/javascript" src="./assets/js/jquery.min.js"></script>
<script type="text/javascript" src="./assets/js/bootstrap.js"></script>
<link rel="stylesheet" href="./assets/css/bootstrap.min.css" type="text/css" >
<link rel="stylesheet" href="./assets/css/bootstrap.rtl.min.css" type="text/css" >
<link rel="stylesheet" href="./assets/css/main.css" type="text/css" >
<link href="./assets/css/fontawesome-all.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" id="logo" href="#">کتابخانه من</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item <?php if($_SERVER['REQUEST_URI'] == '/cms/' || $_SERVER['REQUEST_URI'] == '/cms/index.php' ){echo 'active';} ?>">
<a class="nav-link" href="index.php">خانه <span class="sr-only"></span></a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
دسته بندی
</a>
<div class="dropdown-menu">
<?php
$select_cat = mysqli_query($connection,"SELECT * FROM book_category");
while($row = mysqli_fetch_array($select_cat)) {
if(isset($_GET['category_name'])){
if($_GET['id'] == $row['category_id']){
$class='active';
}else{
$class='';
}
}else{
$class='';
}
echo '<a class="dropdown-item '.$class.'" href="book_category.php?id='.$row['category_id'].'">'.$row['category_name'].'</a>';
}
?>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#">داغ ترین ها</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">نویسندگان</a>
</li>
<?php if(!isset($_SESSION['user'])) : ?>
<li class="nav-item">
<a class="nav-link" href="register.php">عضویت</a>
</li>
<?php endif; ?>
</ul>
</div>
</nav>
<div class="p-2"></div>
and this is my category.php for displaying specific categories posts. 这是我的category.php,用于显示特定类别的帖子。
<?php include 'includes/header.php'; ?>
<div class="container">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">آخرین کتاب ها</a>
</nav>
<article class="row">
<section class="col-lg-8">
<div class="card-columns">
<?php
$result = mysqli_query($connection,"SELECT * FROM books WHERE cat_id = ");
while($row = mysqli_fetch_array($result)) :
?>
<div class="card">
<img class="card-img-top rounded mx-auto d-block" src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>" width="199" height="312">
<div class="card-body text-right">
<h3 class="card-title"><a href="post.php?id=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a></h3>
<p class="card-text"><?php echo mb_substr($row['description'],0,250); echo '...'; ?></p>
<a href="book.php?id=<?php echo $row['id']; ?>" class="btn btn-primary">ادامه مطلب</a>
</div>
</div>
<?php endwhile; ?>
</div>
</section>
<?php include 'includes/sidebar.php'; ?>
</article>
</div>
<div style="width:50px;height:50px;"></div>
<?php include 'includes/footer.php'; ?>
</body>
</html>
You should search the internet for many to many relationships: 您应该在互联网上搜索许多关系:
One book may have zero or more categories. 一本书可能具有零个或多个类别。 On the other side one category may have zero or more book relations.
另一方面,一个类别可能具有零个或多个书本关系。 This relation is called many to many .
这种关系被称为多对多 。
To archive this goal you have to add an mapping table: 要归档此目标,您必须添加一个映射表:
table: book table: book_category table: category
========== ==================== ===============
- id - book_id - id
- name - category_id - name
- author - ...
- ...
To query the data there are examples here how to query many-to-many? 要查询数据,这里有一些示例如何查询多对多? .
。 You should find many examples for php in the internet ...
您应该在互联网上找到许多php的示例...
Remark: OPs question switches between book and post entity. 备注:OP的问题在书本和邮寄实体之间切换。 I decided to use book ... ;)
我决定使用书...;)
OPs question in comment: how to add row for submitted book ...? OPs评论中的问题: 如何为提交的书添加行...?
You have to know the relevant book and category ids. 您必须知道相关的书籍和类别ID。 To connect a book to a catetory, you simply add the id's to the join table.
要将书籍连接到目录,您只需将ID添加到联接表中。
INSERT INTO book_category(book_id,category_id) values(40,50);
In this example the book with id 40 is joined to the category id 50. 在此示例中,将ID为40的书加入目录ID为50。
If your question is targeting an php example, I can't tell you because I don't know how your DAO layer is implemented. 如果您的问题针对的是php示例,那么我不会告诉您,因为我不知道您的DAO层是如何实现的。 But this should be a new question I guess.
但这应该是一个新问题。
If a book always has one category you can simply add a field category_id
to your book's table, but when a book can have multiple categories and you don't know how much. 如果一本书总是具有一个类别,则可以简单地在该表的表中添加一个
category_id
字段,但是当一本书可以具有多个类别而又不知道有多少个类别时。 You can do best by creating a second table that connects with multiple rows of multiple categories. 通过创建第二个表将多个类别的多行连接起来,可以做到最好。
Create a table books_category
with fields like id
, books_id
, books_category_id
. 使用
id
, books_id
, books_category_id
类的字段创建一个books_category
表。 Also make indexes on books_id
, books_category_id
for fast searching. 还要在
books_id
, books_category_id
上books_id
索引以快速搜索。
For your Query, you should use the INNER JOIN
. 对于您的查询,您应该使用
INNER JOIN
。
SELECT * FROM `books` INNER JOIN `books_category` ON `books_category`.`books_id` = `books`.`id` WHERE `books_category`.`books_category_id` = 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.