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

  • Home
  • SEARCH
  • 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 7766177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:20:50+00:00 2026-06-01T15:20:50+00:00

Sorry for bad english and bad title! I have the table post id title

  • 0

Sorry for bad english and bad title!

I have the table “post”

id    title
1     test Thread
2     hello
3     just

so have “tags”

tagid   tagname
1       test
2       russia
3       new site

so have a post_tags

tagid    postid
1        1
2        1
3        1

I need an array from var_dump next below:

$posts = array(
    1 => array(
        'title' => 'test Thread',
        'tags' => array(
            'test', 'russia', 'new site',
        ),
    ),
    2 => array(
        'title' => 'hello',
        'tags' => NULL
    ),
    3 => array(
        'title' => 'just',
        'tags' => NULL
    ),
)

I trying do it, but i getting not that what i want.

SELECT `post`.`id`, `post`.`title`, `tags`.`tagname` FROM `post` 
LEFT JOIN `post_tags` ON `post_tags`.`tagid` = `post`.`id` 
LEFT JOIN `tags` ON `post_tags`.`tagid` = `tags`.`tagid`

I getting in SQL next following:

id  title            tagname
1   test Thread     test
1   test Thread     russia
1   test Thread     newsite
2   hello           NULL
3   just            NULL

PHP

$query = mysql_query("SELECT `post`.`id`, `post`.`title`, `tags`.`tagname` FROM `post` 
    LEFT JOIN `post_tags` ON `post_tags`.`tagid` = `post`.`id` 
    LEFT JOIN `tags` ON `post_tags`.`tagid` = `tags`.`tagid`");
$posts = array();
while ($row = mysql_fetch_assoc($query))
{
    $posts[] = $row;
}

var_dump($posts);

Thank you!!!

  • 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-06-01T15:20:52+00:00Added an answer on June 1, 2026 at 3:20 pm

    The query is fine. You just need some logic in your loop:

    while ($row = mysql_fetch_assoc($query))
    {
        if (isset($posts[$row['id']])) {
            $posts[$row['id']]['tags'][] = $row['tagname'];
        }
        else {
            $posts[$row['id']] = array(
                'title' => $row['title'],
                'tags' => $row['tagname'] === null ? null : array($row['tagname'])
            );
        }
    }
    

    If you have already seen a row with the same post id then all you want from the current row is the tag name (so add this to the “tags” array). If it’s the first time a row with this post id is seen just add it to $posts, being a little careful to set “tags” to either null or an array with one element.

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

Sidebar

Related Questions

Sorry for my bad English. Just like what I asked. I have this class
Hello (Sorry for my bad English) Is it bad practice to assign object to
First, sorry for my bad english, I'm French. Here the problem : I have
First, im sorry to my bad english :). My object have one variable of
Sorry for fuzzy post title, I can't formulate the correct name in English for
Alright, the title sound rubbisch, i know. (and sorry for the bad english) I'm
I am sorry for my bad english. I just try to description my question.
First of all: Sorry for my bad English! I know the title isn't the
First, sorry for my bad english. I have an EF entity that looks like:
I`m from Russia, so sorry for bad English. I want to load template in

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.