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 911641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:14:36+00:00 2026-05-15T17:14:36+00:00

I’ve made some search on the forum without any good answers for my problem.

  • 0

I’ve made some search on the forum without any good answers for my problem. If I missed something, feel free to link me to the question!

What I need to do is simple: a function that returns an array of the full tree of my categories and items. I only have 1 depth (item and a cat_id), so no recursion involved (though if you have a recursive solution, I would gladly accept it).

Right now, I’ve done this, but it’s pretty bad, since I do multiple queries…

function build_tree()
{
    global $wpdb;

    $cats = $wpdb->get_results("SELECT * FROM wp_catering_cats");


    foreach($cats as &$cat)
    {
      $id = $cat->id;

      $cat->items = $wpdb->get_results("SELECT * FROM wp_catering_items WHERE cat_id = $id");
    }

    return $cats;
}

My tables are really simple:

wp_catering_items

id, cat_id, name, price

wp_catering_cats

id, name

Here is an exemple the results array I want:

    Array
    (
        [0] => array
            (
                [id] => 1
                [name] => Cat #1
                [items] => Array
                    (
                        [0] => array
                            (
                                [id] => 1
                                [cat_id] => 1
                                [name] => Item #1
                                [price] => 5
                            ),
                        ...

                    )

            ),
           ...
   );

If something is not clear, feel free to comment!

Thanks!

EDIT

I’ve made some modifications using the code bellow, but I’ pretty sure there’s a neater way to do this. Having to order one DESC and one ASC just doesn’t sounds right..

function build_tree()
{
    global $wpdb;

    $cats = $wpdb->get_results("SELECT * FROM wp_catering_cats ORDER BY id DESC");
    $items = $wpdb->get_results("SELECT * FROM wp_catering_items ORDER BY cat_id ASC");

    $item = array_pop($items);

    foreach($cats as &$cat)
    {   
        while($item->cat_id == $cat->id)
        {
            $cat->items[] = $item;
            $item = array_pop($items);
        }
    }

    print_r($cats);
}
  • 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-05-15T17:14:37+00:00Added an answer on May 15, 2026 at 5:14 pm

    If you are just trying to optimize, then do the simple thing, instead of only grabbing the items for the specific cat you are on, grab all the items at once, and order them by catID. Then loop through your cats, and pop items off your item results until you hit the next cat.

    function build_tree()
    {
        global $wpdb;
    
        $cats = $wpdb->get_results("SELECT * FROM wp_catering_cats order by cat_id asc");
        $items = $wpdb->get_results("SELECT * FROM wp_catering_items ORDER BY cat_id asc");
    
        foreach($cats as &$cat)
        {
          $id = $cat->id;
          $item = array_pop($items)
          while($item['cat_id'] == $id)
          {
            $cats->item[] = $item;
            $item = array_pop($items)
          }
          #do a little bookkeeping so you next cat gets its first item, and zero item cats get skipped.
    
        }
    }
    

    Update: Thanks for the comment.. Forgot to add the pop in the while loop!

    Second update: use array_shift instead of array_pop if you don’t want reverse ordering to be a problem…

    • 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 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 have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.