I’m having some major issues trying to grab all the data I need for a SQL query. I’m still new with queries, so I will try to describe this as best as possible.
I am trying to do a cross query with the WordPress plugin NextGen Gallery. Basically there are two tables nggalbum and nggallery. What I’m attempting to do is create a nested list of all albums and galleries.
The data in nggalbum contains columns id, name, slug, previewpic, albumdesc, sortorder, and pageid. The only values that I’m interested in are id, name, slug, and sortorder. The value for sortorder is serialized data that has the relationship data of this entry and all other album and gallery entries. For example: a:2:{i:0;s:2:"a2";i:1;s:2:"a6";} This basically means the current query item has two sub-albums (their corresponding id prefixed with an “a”): a2 and a6. If it has galleries, the number doesn’t have an a prefix and is the ngggallery gid (will cover that in a second).
I use this to grab all the data from the nggalbum table:
$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 );
}
Sample data:
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
)
)
I add an a prefix to all of these id’s because they are albums and I figured this may help later. Since I’m not sure what I’m doing, this may not be the case.
nggallery contains columns gid, name, slug, path, title, galdesc, pageid, previewpic, and author. The only relevant columns are gid, name, slug, path, and title.
I use this to grab all the data from the nggallery table:
$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;
}
Sample data:
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
)
Now this is where I get stuck. I would really love to be able to write some code to parse through each album’s galleries array and associate it with the corresponding albums and/or galleries from nggallery.
Eventually I would love to achieve a nested list of albums/galleries, such as:
(a1) [link] Title
(a2) [link] Title
1 [link] Title
2 [link] Title
3 [link] Title
(a3) [link] Title
1 [link] Title
[...]
I’m not entirely sure how to even start going about this. I tried looping some things through some foreach statements and have been largely unsuccessful. I would search google for this but I have no idea what this technique is even called.
I would REALLY like to understand how to do something like this, so if you could please shed any light upon this technique, I would be greatly indebted. Links to similar tutorials or just basic concepts would be very beneficial to me. I don’t expect anyone to do all the code for me, but any step in the right direction would be greatly appreciated ( and if you want to do the code, with some steps, I won’t argue of course 😉 ).
Thanks so much ahead of time!
Tre
I don’t quite understand how albums and galleries are related to each other and what you want in the nested list. However, it looks to me like the problem is in the “sortorder” column which is doing too much. I suspect you are trying to express a many-to-many relationship between your tables in which case it might be cleaner to have a separate table that expresses that relationship. Once you’ve done that I think you can more easily query which nalbums and ngalleries relate to gallery. Does that help much?
Edit:
OK so I think I understand now. You are trying to create a hierarchy of albums and you want to print the whole hierarchy of albums including all the galleries that each album has. So, using your existing design I think you could do something like this: