Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1047081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:14:50+00:00 2026-05-16T16:14:50+00:00

My script is supposed to find all related posts from the current posts categories

  • 0

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>';
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:14:50+00:00Added an answer on May 16, 2026 at 4:14 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script (python) supposed to build up two paths - one in
I'm working on a script that is supposed to install some software from a
How can I find what's hanging all new installations on a Windows box? While
I'm creating a really Find/Replace System but one of the main features are not
I have a PHP script that's supposed to take in a URL and search
I'm writing a script that is supposed to run around a bunch of servers
I have a script that is supposed to sit there, happily running in a
I am writing a bash script that is supposed to do some confirmation and
I have a function in a powershell script that is supposed to untar my
I have a Ruby 1.8.6 script that is supposed to take a filename that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.