Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7901361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:12:00+00:00 2026-06-03T09:12:00+00:00

I’m having some major issues trying to grab all the data I need for

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:12:01+00:00Added an answer on June 3, 2026 at 9:12 am

    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:

    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 */
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.