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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:33:23+00:00 2026-06-19T03:33:23+00:00

So I have a table in a database filled with categories structured like this:

  • 0

So I have a table in a database filled with categories structured like this:

pk int: cat_id
int: parent_id
varchar: cat_name

A top level category has a null parent_id and there could be several levels of children.
Basically, I am taking a search string and matching it against the category names, and if the category has parents then I want to display the full path in breadcrumb fashion for all matches. Here is pseudo code for what I am doing now:

search_string = whatever;
result = query db and find all category names matching "%whatever%";
foreach(result)
{
    parent_id = result[parent_id];
    result_path = result[cat_name];
    while(parent_id != null)
    {
        result2 = query db and find cat_id = parent_id;
        result_path = result2[cat_name] + " > " + result[path];
        parent_id = result2[parent_id];
    }
    print result_path;
}

Is there a more efficient way to do this other than the nested loop and database queries?

Thanks for any input.

  • 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-19T03:33:25+00:00Added an answer on June 19, 2026 at 3:33 am

    What you have is a hierarchy stored as an adjacency list. Recursion is the only practical way to solve this, either in MySql or PHP.

    If you are seriously looking for efficiencies for this particular query then store your hierarchy as nested sets. Be aware that this is a non-trivial exercise and will increase the cost of maintaining your data. It will also reduce the efficiency of other queries such as finding children.

    There is plenty of info on this site and others about storing hierarchical data in RDMS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have database table like this +-------+--------------+----------+ | id | ip | date |
that my problem: I have database table like that: id (AI) market_id 1 6
I have a database table named Categories . I want to create a form
I have a JTable filled with data of a table of my database (so
I have a table filled with data from my database, and i want a
I have a table which is filled from a database. The user can delete
I have a table that is filled with data from a database. When you
I have a mysql database table filled with 1000+ records, lets say 5000 records.
I have a table 2x2 (2 columns and 2 rows filled) on my database,
So I have this table filled with a row (we call it $numbers for

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.