I'm working on a custom 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
Question 1: How to store book in database with multiple categories? Should I store category ids as a string value in database?
Question 2: How to query posts with multiple categories ?
This is my header.php code
<?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.
<?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>