Isis
Isis

Reputation: 4666

MySQL Select FROM 3 tables AND put that in PHP array

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

Upvotes: 4

Views: 522

Answers (3)

dqhendricks
dqhendricks

Reputation: 19251

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'];
}

Upvotes: 2

Alon Eitan
Alon Eitan

Reputation: 12025

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

Upvotes: 1

Jon
Jon

Reputation: 437554

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.

Upvotes: 2

Related Questions