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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:07:11+00:00 2026-05-12T06:07:11+00:00

With only a bit of previous experience with databases and no formal education with

  • 0

With only a bit of previous experience with databases and no formal education with them, I’m a bit stuck as to how to model this (and retrieve the data I require from it in PHP). This is what I’m trying to model:

For each item on my site, it is allowed to have multiple tags such as file, upload, php, recursive etc. However the tags are reusable, I can have two different items and they each could have the php tag.

I’ve been trying to read up on how to do this and whether it is lack of experience with it or something else I don’t know, but I can’t seem to grasp the concept. Apparently you need a middle table which links the two together?

Also once I have this relationship and the tables defined, how would I do things such as:
– Retrieve all items with a certain tag?
– Retrieve all tags that one item has?

Thanks for your help, also if anyone could list any further reading on this to strengthen my understanding of the concept that would be great.

  • 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-12T06:07:12+00:00Added an answer on May 12, 2026 at 6:07 am

    The db part is easy. This is just a sample so you can see how db can look like, not any particular SQL engine queries.

    CREATE TABLE posts (
        id INT PRIMARY KEY,
        subject VARCHAR(100),
        body TEXT
    )
    
    CREATE TABLE tags (
        id INT PRIMARY KEY,
        name VARCHAR(50)
    )
    
    CREATE TABLE post_tags (
        post_id INT,
        tag_id INT,
        FOREIGN KEY (post_id) REFERENCES posts (id),
        FOREIGN KEY (tag_id) REFERENCES posts (id)
    )
    

    To get items with yourTag tag you will just run query like this

    SELECT P.*
    FROM posts P 
        LEFT JOIN post_tags PT ON (PT.post_id = P.id)
        LEFT JOIN tags T ON (T.id = PT.tag_id)
    WHERE T.name = 'yourTag';
    

    To get tags associated with post with id of 123 you run this query:

    SELECT T.*
    FROM tags T 
        LEFT JOIN post_tags PT ON (T.id = PT.tag_id)
        LEFT JOIN posts P ON (PT.post_id = P.id)
    WHERE P.id = 123;
    

    For the PHP part you could use a framework. Many (if not all) frameworks can easily model such relationships. For example in CakePHP this done like that:

    class Post extends AppModel {
        $useTable = 'posts';
        $hasAndBelongsToMany = array(
            'Tag' => array(
                'className' => 'Tag'
                'joinTable' => 'post_tags'
                'foreignKey' => 'post_id'
                'associationForeignKey' => 'tag_id'
            )
        );
    }
    
    class Tag extends AppModel {
        $useTable = 'tags';
        $hasAndBelongsToMany = array(
            'Post' => array(
                'className' => 'Post'
                'joinTable' => 'post_tags'
                'foreignKey' => 'tag_id'
                'associationForeignKey' => 'post_id'
            )
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[Edit: This problem applies only to 32-bit systems. If your computer, your OS and
I only read a little bit about this topic, but it seems that the
How do I change this bit of code so that I only allow pdf
(This is a follow-up from this previous question ). I was able to successfully
Following my previous question (I assume that 64-bit compiler uses only SSE instructions for
This is a follow up question from a previous SO question . Now I
I've gotten help from others on this, but their replies were a little bit
First off I'd like to say this is the only java experience I've had.
Java SE 6 (64 bit only) is now on OS X and that is
I've only done a bit of Flex development thus far, but I've preferred the

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.