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 6712003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:12:30+00:00 2026-05-26T08:12:30+00:00

I’m trying to get all the categories about the specific blogpost but it will

  • 0

I’m trying to get all the categories about the specific blogpost but it will not work as I wanted it to work. Below you can see the SQL and code. $blog fetch the information about the specific blogpost.

CREATE TABLE IF NOT EXISTS `blogposts` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `id_user_created` int(10) DEFAULT '0',
  `id_user_edited` int(10) DEFAULT '0',
  `post_subject` varchar(100) NOT NULL,
  `post_message` text NOT NULL,
  `post_categories` text NOT NULL,
  `date_published` datetime NOT NULL,
  `date_edited` datetime NOT NULL,
  `info_ipaddress_created` text NOT NULL,
  `info_ipaddress_edited` text NOT NULL,
  `is_shared` enum('0','1') DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
)


CREATE TABLE IF NOT EXISTS `categories` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `id_user` int(10) DEFAULT '0',
  `post_name` text NOT NULL,
  `date_added` datetime NOT NULL,
  `date_edited` datetime NOT NULL,
  `info_ipaddress` text NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
)


$get_categories = "SELECT * FROM blogposts, categories
                   WHERE blogposts.id = '".(int)$blog['id']."'
                   AND blogposts.post_categories = categories.id
                  ";

foreach($sql->query($get_categories) AS $category) {
    echo '<a href="'.url('blog/category/'.$category['post_name']).'" class="grey-link">';
        echo $category['post_name'];
    echo '</a>';
}


INSERT INTO blogposts` (id, id_user_created, id_user_edited, post_subject, post_message, post_categories, date_published, date_edited, info_ipaddress_created, info_ipaddress_edited, is_shared)
VALUES (1, 1, 1, 'test', 'testar', '1', '', '', '', '', '0'),
(2, 1, 1, 'med kategorier', 'wiho!', '1|2', '', '', '', '', '0');

INSERT INTO categories` (id, id_user, post_name, date_added, date_edited, info_ipaddress)
VALUES (1, 1, 'test', '', '', ''),
(2, 1, 'test2', '', '', '');

Right now it’s only prints Categorized in test, on both blogposts I have posted. It should prints out Categorized in test for the first post and Categorized in test, test2 for the second post. post_categories looks like this: 1|2 which is the ID for the specific category.

Thanks in advance.

  • 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-26T08:12:31+00:00Added an answer on May 26, 2026 at 8:12 am

    If I look at your ‘blogposts’ table, your ‘post_categories’ field is of type text. I don’t know what is in there but I can imagine it to be a string with id’s separated by some character like a semi-colon. This approach limit’s the flexibility of your queryies. You’d be better of by creating a link table. This means that you introduce a new table that is responsible for the linkage between a blogpost and it’s categories. Looking something like:

    CREATE TABLE IF NOT EXISTS `blogposts` ( 
      `id` int(10) NOT NULL AUTO_INCREMENT, 
      `post_message` text NOT NULL,
      PRIMARY KEY (`id`), 
      UNIQUE KEY `id` (`id`) 
    ) 
    
    CREATE TABLE IF NOT EXISTS `categories` ( 
      `id` int(10) NOT NULL AUTO_INCREMENT, 
      `category_name` text NOT NULL, 
      PRIMARY KEY (`id`), 
      UNIQUE KEY `id` (`id`) 
    ) 
    
    CREATE TABLE IF NOT EXISTS `post_categories` ( 
      `post_id` int(10) NOT NULL AUTO_INCREMENT, 
      `category_id` text NOT NULL
    ) 
    

    This last table can be used to get all categories that come with a blogpost. You can query over it by using a JOIN, like so:

    SELECT * 
    FROM blogposts b
    INNER JOIN post_categories pc ON pc.post_id=p.id
    INNER JOIN categories c ON c.id=pc.category_id
    WHERE b.id=<yourvariable>
    

    You will than have a list with a lot of duplication but each row is for another category which you can use to display somewhere or whatever purpose you have with it. Maybe it is cleaner to separate this into two queries that a. get the blogpost with the id you have and b. get the categories that come with the blogpost by using the link table.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.