4

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!!!

2
  • you cannot get a multi-dimensional arrays back from a mysql database. you must do your own post processing to the results if you want it in that form. Commented Apr 10, 2012 at 21:38
  • 1
    FYI: escaping table/field names is only required if you're using reserved words. Dumping everything into backticks just makes the query that much harder to read. Commented Apr 10, 2012 at 21:45

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

you cannot get a multi-dimensional arrays back from a mysql database. you must do your own post processing to the results if you want it in that form. Something like this maybe?

$posts = array();
while ($row = mysql_fetch_assoc($query))
{
    if (!isset($posts[$row['id']])) {
        $posts[$row['id']] = array();
        $posts[$row['id']]['title'] = $row['title'];
        $posts[$row['id']]['tags'] = array();
    }
    if ($row['tagname'] != null) $posts[$row['id']]['tags'][] = $row['tagname'];
}

Comments

1

Try this:

while ($row = mysql_fetch_assoc($query))
{
    if( !isset( $posts[$row["id"]] ) ) {
        $posts[ $row["id"] ] = array( "title" => $row["title"], "tags" => array() );
    }
    array_push( $posts[ $row["id"] ][ "tags" ], $row["tagname"] );
}

I can't debug it, so tell me if you get any errors

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.