MySQL连接两个带有序列化数据的表,递归?

时间:2012-05-08 02:16:35

标签: mysql join recursion associative-array

我在尝试获取SQL查询所需的所有数据时遇到了一些重大问题。对于查询我还是新手,所以我会尝试尽可能最好地描述它。

我正在尝试使用Wordpress插件NextGen Gallery进行交叉查询。基本上有两个表nggalbumnggallery。我正在尝试做的是创建一个包含所有专辑和画廊的嵌套列表。

nggalbum中的数据包含idnameslugpreviewpicalbumdescsortorder列,以及pageid。我感兴趣的唯一值包括idnameslugsortordersortorder的值是序列化数据,其中包含此条目的关系数据以及所有其他相册和图库条目。例如:a:2:{i:0;s:2:"a2";i:1;s:2:"a6";}这基本上意味着当前查询项目有两个子相册(其对应的ID前缀为“a”):a2a6。如果它有图库,则该编号没有a前缀,而是ngggallery gid(将在一秒内覆盖)。

我用它来获取nggalbum表中的所有数据:

    $albums = $wpdb->get_results("SELECT * FROM $wpdb->nggalbum" , OBJECT_K );

    foreach ($albums as $key => $value) {
        $albums[$key]->id = 'a' . $key;
        $albums[$key]->galleries = empty ($albums[$key]->sortorder) ? array() : (array) unserialize($albums[$key]->sortorder)  ;
        $albums[$key]->name = stripslashes( $albums[$key]->name ); 
        $albums[$key]->albumdesc = stripslashes( $albums[$key]->albumdesc );
    }

示例数据:

Array
(
[1] => stdClass Object
    (
        [id] => a1
        [name] => Image Gallery
        [slug] => image-gallery
        [previewpic] => 0
        [albumdesc] => 
        [sortorder] => a:2:{i:0;s:2:"a2";i:1;s:2:"a6";}
        [pageid] => 0
        [galleries] => Array
            (
                [0] => a2
                [1] => a6
            )

    )

[2] => stdClass Object
    (
        [id] => a2
        [name] => ALBUM 1 - High res
        [slug] => album-1-high-res
        [previewpic] => 0
        [albumdesc] => 
        [sortorder] => a:2:{i:0;s:1:"2";i:1;s:1:"3";}
        [pageid] => 0
        [galleries] => Array
            (
                [0] => 2
                [1] => 3
            )

    )

我为所有这些ID添加了a前缀,因为它们是专辑,我想出这可能会有所帮助。由于我不确定我在做什么,情况可能并非如此。

nggallery包含gidnameslugpathtitlegaldesc,{{1}列},pageidpreviewpic。唯一相关的列是authorgidnameslugpath

我用它来获取title表中的所有数据:

nggallery

示例数据:

$galleries = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->nggallery", OBJECT_K );

foreach ($galleries as $key => $value) {
    $galleriesID[] = $key;
    $galleries[$key]->counter = 0;
    $galleries[$key]->title = stripslashes($galleries[$key]->title);
    $galleries[$key]->galdesc  = stripslashes($galleries[$key]->galdesc);
    $galleries[$key]->abspath = WINABSPATH . $galleries[$key]->path;     
}

现在这就是我被卡住的地方。我真的很想能够编写一些代码来解析每个专辑的Array ( [2] => stdClass Object ( [gid] => 2 [name] => new_collection [slug] => new-collection [path] => wp-content/gallery/new_collection [title] => NEW COLLECTION [galdesc] => [pageid] => 0 [previewpic] => 8 [author] => 1 [counter] => 0 [abspath] => /Applications/MAMP/htdocs/igal/wp-content/gallery/new_collection ) [3] => stdClass Object ( [gid] => 3 [name] => cha-collection [slug] => cha-collection [path] => wp-content/gallery/cha-collection [title] => CHA COLLECTION [galdesc] => [pageid] => 0 [previewpic] => 15 [author] => 1 [counter] => 0 [abspath] => /Applications/MAMP/htdocs/igal/wp-content/gallery/cha-collection ) 数组,并将其与来自galleries的相应专辑和/或画廊相关联。

最终我希望获得一个嵌套的专辑/画廊列表,例如:

nggallery

我不完全确定如何开始这个。我尝试通过一些(a1) [link] Title (a2) [link] Title 1 [link] Title 2 [link] Title 3 [link] Title (a3) [link] Title 1 [link] Title [...] 语句循环一些事情,并且基本上没有成功。我会搜索谷歌这个,但我不知道这种技术甚至被称为。

真的喜欢理解如何做这样的事情,所以如果你能够对这种技术有所了解,我会非常感激。链接到类似的教程或只是基本概念对我来说非常有益。我不希望任何人为我做所有的代码,但是在正确的方向上任何一步都会非常感激(如果你想做代码,有一些步骤,我当然不会争论;))。

非常感谢你!

1 个答案:

答案 0 :(得分:1)

我不太明白专辑和画廊是如何相互关联的,以及你想在嵌套列表中想要什么。但是,在我看来问题是在“sortorder”列中做了太多。我怀疑你试图表达你的表之间的多对多关系,在这种情况下,拥有一个表达该关系的单独表可能更清晰。一旦你完成了,我认为你可以更容易地查询哪些nalbums和ngalleries与gallery相关。这有用吗?

编辑:

好的,我想我现在明白了。您正在尝试创建专辑层次结构,并且您希望打印整个专辑层次结构,包括每个专辑所具有的所有图库。所以,使用你现有的设计,我认为你可以这样做:

PrintGallery( string galleryID ) 
{
   //1 do a query that selects the gallery using the id (use a where clause)

   //2 print gallery details like the name etc whatever you want to 
}

PrintAlbum ( string albumID )
{
   //1 do a query that selects the Album using the id (use a where clause)
   //2 print Album details (name icon etc) but not the gallery array details.

   if(galleries array length > 0 ) 
     /* in case there are no galleries we don't want a hanging <li> with nothing in it */
     echo "<ul>" /* This will ensure the items are nested properly */
     foreach item currentItemID in galleries array
        echo "<li>"
        if (currentItemID is an Album)
            PrintAlbum(currentItemID) /* recurse */
        else /* assume it's a gallery are in the array */
            PrintGallery(currentItemID) 
     echo "</ul>" /* end this level */

}