0

我在尝试获取 SQL 查询所需的所有数据时遇到了一些重大问题。我对查询还是陌生的,所以我会尽可能地描述这一点。

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

中的数据nggalbum包含idnameslugpreviewpicalbumdescsortorder、 和列pageid。我感兴趣的唯一值是idnameslugsortorder。的值sortorder是具有此条目以及所有其他专辑和画廊条目的关系数据的序列化数据。例如: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包含列gid, name, slug, path, title, galdesc, pageid, previewpic, 和author. 唯一相关的列是gidnameslugpathtitle

我用它来获取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

    [...]   

我不完全确定如何开始做这件事。我尝试通过一些语句循环一些东西,但foreach基本上没有成功。我会在谷歌上搜索这个,但我不知道这种技术叫什么。

真的很想了解如何做这样的事情,所以如果你能对这种技术有所了解,我将不胜感激。指向类似教程或只是基本概念的链接对我非常有益。我不希望任何人为我编写所有代码,但我们将不胜感激朝着正确方向迈出的任何一步(如果你想编写代码,通过一些步骤,我当然不会争辩;))。

提前非常感谢!

特雷

4

1 回答 1

1

我不太明白专辑和画廊是如何相互关联的,以及您在嵌套列表中想要什么。但是,在我看来,问题出在“排序顺序”列中,该列做得太多了。我怀疑您正在尝试表达表之间的多对多关系,在这种情况下,使用单独的表来表达该关系可能会更清晰。完成此操作后,我认为您可以更轻松地查询哪些相册和画廊与画廊相关。这有很大帮助吗?

编辑:

好的,我想我现在明白了。您正在尝试创建专辑层次结构,并且想要打印专辑的整个层次结构,包括每个专辑拥有的所有画廊。因此,使用您现有的设计,我认为您可以执行以下操作:

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 */

}
于 2012-05-08T02:39:27.403 回答