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>';
}
}