简体   繁体   中英

PHP & MySQL display problem

My script is supposed to find all related posts from the current posts categories which can be one or many categories but I want to stop the script from displaying the current post as a related post. How can I do this? And where do I add it to my script?

Here is my MySQL table.

CREATE TABLE categories ( 
id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
parent_id INT UNSIGNED NOT NULL DEFAULT 0, 
category VARCHAR(255) NOT NULL, 
url VARCHAR(255) NOT NULL,
PRIMARY KEY (id), 
INDEX parent (parent_id),
UNIQUE KEY(parent_id, url)
);

CREATE TABLE users_posts (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT UNSIGNED NOT NULL,
title TEXT NOT NULL,
summary TEXT DEFAULT NULL,
post_content LONGTEXT NOT NULL,
PRIMARY KEY (id)
);

Here is my script forgive the mess.

$posts_categories = array();
$ac_query = mysqli_query($mysqli, "SELECT category_id 
                                   FROM posts_categories
                                   WHERE post_id = '" . $post_id . "'");

if (!$ac_query) {
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_array($ac_query)){
        $posts_categories[] = $row['category_id'];
    }
}

$posts_categories_name = array();
$acn_query = mysqli_query($mysqli, "SELECT category 
                                    FROM categories 
                                    WHERE id IN(" . implode(',', $posts_categories) . ")"); 

if (!$acn_query) {
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_array($acn_query)){
        $posts_categories_name[] = $row['category'];
    }
}

$x2 = '';
$c2 = '';
foreach($posts_categories_name as $acn) {
    $x2++;
    if($x2 == 1){
        $c2 .= " users_posts.title LIKE '%$acn%' OR users_posts.summary LIKE '%$acn%' OR users_posts.post_content LIKE '%$acn%'";
    } else {
        $c2 .= " OR users_posts.title LIKE '%$acn%' OR users_posts.summary LIKE '%$acn%' OR users_posts.post_content LIKE '%$acn%'";
    }
}

$rac_query = mysqli_query($mysqli, "SELECT *
                                    FROM users_posts
                                    WHERE $c2 
                                    ORDER BY RAND() 
                                    LIMIT 5");
if (!$rac_query) {
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_array($rac_query)){ 
        echo '<li>' . $purifier->purify(strip_tags($row['title'])) .'</li>';
    }
}

The easiest way is to hold the current post's id in a variable (we'll call it $current_post_id) then just:


if (!$rac_query) {
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_array($rac_query)){
        if($current_post_id == $row['id']){
            continue;
        }
        echo '<li>' . $purifier->purify(strip_tags($row['title'])) .'</li>';
    }
}

That's the last bit of code you posted, BTW. The "continue" means skip ahead to the next run of the while loop.

Of course you could also do it the easy way and say if $current_post_id != $row['id'] then echo out the line. It would do the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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