显示同一类别的相关帖子。使用 PHP 和 MySQL

Display related posts from the same category. Using PHP and MySQL

我在 PHP 和 MySQL 中有一个博客站点,包含两个 table、POST 和类别。 我需要帮助,如何在访问者单击单个 post 标题时显示来自同一类别的相关 post。非常感谢您的帮助

POST table

POST_ID CAT_ID TITLE DESCRIPTION IMAGE
1 3
2 5
3 1
4 6

类别table

Cat_id Cat_name Total_post
3 National 5
5 International 7
1 Sports 3
6 Technology 2

这是我的代码:

<?php

if (isset($_GET['POST_ID'])) {
    $POST_ID = $_GET['POST_ID'];
}

$sql1 = "select * from POST where POST_ID=$POST_ID";
$result1 = mysqli_query($conn, $sql1);

if (mysqli_num_rows($result1) > 0) {
?>

    <div class="col-lg-4">
        <div class="sidebar">
            <div class="sidebar-widget">

                <h2 class="sw-title">Related Posts</h2>

                <div class="news-list">

    <?php while ($row = mysqli_fetch_assoc($result1)) { ?>
                    
                    <div class="nl-item">
                        <div class="nl-img">

                                <img src="<?php if (file_exists("upload/" . $row['image'])) {
                                                echo "upload/" . $row['image'];
                                            } else {
                                                echo "upload/" . $row['image'];
                                            } ?>" alt="" class="img-fluid">

                        </div>

                        <div class="nl-title">

                            <h4>
                                <a href="single-page.php?POST_ID=<?= $row['POST_ID']; ?>" title="<?= $row1['title']; ?>"><?= $row1['title']; ?></a>
                            </h4>

                        </div>
                    </div>
    <?php } ?>
                </div>
            </div>
        </div>
    </div>

<?php } ?>

正在重新创建您的数据库

下次您应该提供代码来重新创建您遇到问题的代码部分!

CREATE TABLE CATEGORIES(
    Cat_id BIGINT PRIMARY KEY AUTO_INCREMENT,
    Cat_name VARCHAR(50),
    Total_post INT
);

CREATE TABLE POST(
    POST_ID BIGINT PRIMARY KEY AUTO_INCREMENT,
    CAT_ID BIGINT,
    TITLE VARCHAR(50),
    DESCRIPTION TEXT,
    FOREIGN KEY (CAT_ID) REFERENCES CATEGORIES(CAT_ID)
);

INSERT INTO
    `categories`(`Cat_id`, `Cat_name`, `Total_post`)
VALUES 
    (3, 'national', 5),
    (5, 'International', 7),
    (1, 'Sports', 3),
    (6, 'Technology', 2);
$db_connection = mysqli_connect($db_host, $db_user, $db_password, $db_name);

解决方案

/* ********************************* */
// GETTING POST ID
/* ********************************* */
$post_id = NULL;

if (!empty($_GET['POST_ID'])) {
    $post_id = intval($_GET['POST_ID']); // Making sure that we received an integer ID
} else {
    echo "A post ID must be provided!";
    die;
}

/* ********************************* */
// FETCHING THE POST
/* ********************************* */

$main_post = mysqli_query(
    $db_connection,
    "SELECT * FROM POST WHERE POST_ID=$post_id"
);

$main_post_data = NULL; // Will become the associative array

if (mysqli_num_rows($main_post) === 1) {
    $main_post_data = mysqli_fetch_assoc($main_post);
} else {
    echo "Post not found";
    die;
}

/* ********************************* */
// FETCHING RELTED POSTS
/* ********************************* */
$category_id = $main_post_data['CAT_ID'];

$related_posts_query = "
SELECT
    *
FROM
    POST
WHERE
    CAT_ID = $category_id
    AND
    POST_ID != $post_id
";
$related_posts = mysqli_query(
    $db_connection,
    $related_posts_query
);

$related_posts_data = [];

if (mysqli_num_rows($related_posts) > 0) {
    $related_posts_data = mysqli_fetch_all($related_posts, MYSQLI_ASSOC);
}

/* ********************************* */
// THE END
/* ********************************* */
echo "<pre>";
print_r($main_post_data);
print_r($related_posts_data);
echo "</pre>";
die;

博客的关系数据库

基本上:

一个post可能属于很多类别

例如最新的windows漏洞post可能属于技术 黑客 类别

一个类别可以引用很多post

例如技术类别可能有最新windows个漏洞前10个PHP 2022 年框架 posts

这需要两个实体之间的桥梁 table。