SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Matching up and consolidating or merging several arrays

    Hi, this issue has to do with a Wordpress plugin, but from the looks of it, it's more in line with this forum than the Wordpress one.

    Basically, I'm having to customize an events/calendar plugin, which I can do outside of the plugin due to it supporting themes, but I've come in to some problems being at the level that I am with php.

    The page I'm trying to edit carries over a post, but does not carry over it's associated tags or categories which I need to display within that page. None of the Wordpress methods for getting this information are working, and when I do a var_dump both fields are showing up but showing up NULL. I know this isn't the case, because there is a dropdown within the same page (although not part of the php page I'm working on) that narrows down events by both categories and tags.

    I've figured out that the best way to get this information is through a SQL query on that page, and then to re-associate the tags and categories to the event so I can echo them out. I've written the query and it is getting the information on to the page, but it's not coming up in the way I need it to, so there is some php coding that I have to do to get it all in line with each other.

    First off...these are the queries I've written. I've had to make two because the categories and tags are in the same field, and I couldn't find a good way with SQL to place them within the same array
    PHP Code:

    $getCat 
    $wpdb->get_results("SELECT 
                    p.ID, 
                    t.name AS category_name
                FROM 
    $wpdb->posts p
                    JOIN 
    $wpdb->term_relationships tr ON p.ID = tr.object_id
                    JOIN mydatabase_ai1ec_events ae ON ae.post_id = p.ID
                    JOIN 
    $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
                    JOIN 
    $wpdb->terms t ON t.term_id = tt.term_id
                WHERE p.post_type =  '" 
    AI1EC_POST_TYPE "'
                AND tt.taxonomy = 'events_categories'
                ORDER BY ae.start ASC
                "
    );            
                
    $getTag $wpdb->get_results("SELECT 
                    p.ID,
                    t.name AS tag_name
                FROM 
    $wpdb->posts p
                    JOIN 
    $wpdb->term_relationships tr ON p.ID = tr.object_id
                    JOIN mydatabase_ai1ec_events ae ON ae.post_id = p.ID
                    JOIN 
    $wpdb->term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
                    JOIN 
    $wpdb->terms t ON t.term_id = tt.term_id
                WHERE p.post_type =  '" 
    AI1EC_POST_TYPE "'
                AND tt.taxonomy = 'events_tags'
                ORDER BY ae.start ASC
                "
    ); 
    So, first off I need to place the categories and the tags within the same array. I've managed to do this to an extent with this code:

    PHP Code:
    foreach($getTag as $k=>$v)
    {
            if(
    array_key_exists($k$getCat))
            {
                
    $taxonomy[$k] = array($v$getCat[$k]);
            }else
        {
            
    $taxonomy[$k] = array($vnull);
        }
        
    print_r ($taxonomy[$k]);
        

    But this only places them as arrays within a larger array, grouped by the ID like I need, but I can't figure out how to pull any of this information out this way.
    They are stdClass Objects, and I'm not sure if that's affecting their behavior or not, as I only have beginner's understanding of php and OOP.
    This is what I get when I do a var_dump.

    Array ( [0] => stdClass Object ( [ID] => 5492 [tag_name] => Broseph Stalin ) [1] => stdClass Object ( [ID] => 5492 [category_name] => Kick Boxing Class ) )
    Array ( [0] => stdClass Object ( [ID] => 5493 [tag_name] => Porridge Bush ) [1] => stdClass Object ( [ID] => 5493 [category_name] => Boxing Class ) )


    Ideally I'd like to place these in an array as such, array(ID, tag_name, category_name), and then after I have that I'd like to echo out the array according to which ID is being called. The event ID is the variable that I need to hook in to in order to display these.

    Can any one offer some guidance as to how I can convert these arrays to something that I can use?

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    My immediate response is "do it all in one query instead"... but the schema of this structure is escaping my brain's ability to comprehend at the moment.

    Anyway; to compile the information (which is a vastly less brain-consuming process.)...
    PHP Code:
    $out = array();
    foreach(
    $getTag as $v) {
      
    $out[$v['ID']]['tag_name'] = $v['tag_name']);
    }
    foreach(
    $getCat as $v) {
      
    $out[$v['ID']]['category_name'] = $v['category_name']);

    You then have an array $out, which is keyed by ID, and each element is an array of 1 or 2 items; tag_name and category_name (there should be a 1:1 matching on these entries, but thats not guaranteed by this logic.) The accessor then is simply, using your data as an example: $out[5492]['tag_name']; or $out[5492]['category_name'];
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •