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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:18:48+00:00 2026-05-21T03:18:48+00:00

I’d like to be able to build the breadcrumbs for a content page, however

  • 0

I’d like to be able to build the breadcrumbs for a content page, however the categories a piece of content is in can have unlimited depth, so i’m not sure how to go about it without getting each category one by one and then getting its parent etc. It seems like it could be a simpler way but I can’t figure it out.

I have an articles table

article_id
article_name
article_cat_id

I also have a categories table

cat_id
cat_name
cat_parent

Cat parent is the id of another category of which a category is a child.

Imagine an article which is 5 categories deep, as far as I can tell i’d have to build the breadcrumbs something like this (example code obviously inputs should be escaped etc)

<?php
$breadcrumbs = array(
    'Category 5',
    'Content Item'
);
$cat_parent = 4;

while($cat_parent != 0) {
    $query = mysql_query('SELECT * FROM categories WHERE cat_id = '.$cat_parent);
    $result = mysql_fetch_array($query, MYSQL_ASSOC);
    array_unshift($breadcrumbs, $result['cat_name']);
    $cat_parent = $result['cat_parent'];
}
?>

This would then give me

array(
'Category 1',
'Category 2',
'Category 3',
'Category 4',
'Category 5',
'Content Item'
)

Which I can use for my breadcrumbs, however its taken me 5 queries to do it, which isn’t really preferable.

Can anyone suggest any better solutions?

  • 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-21T03:18:48+00:00Added an answer on May 21, 2026 at 3:18 am

    Here are some easy options in order of simplicity:

    1. Stick with the design you have, use the recursive/iterative approach and enjoy the benefits of having simple code. Really, this will take you pretty far. As a bonus, it is easier to move from here to something more performant, than from a more complicated setup.

    2. If the nr of categories isn’t very large, you can select all of them and build the hierarchy in PHP. Due to pagesize the amount of work required to fetch 1 rows vs a whole bunch of them (say a few hundred) is pretty much the same. This minimizes the nr of queries/network trips, but increases the amount of data transported over the cable. Measure!

    3. Cache the hierarchy and reload it entirely every X unit of time or whenever categories are added/modified/deleted. In it’s simplest form, the cache could be a PHP file with a nested variable structure containing the entire category hierarchy, along with a simple index for the nodes.

    4. Create an additional table in which you have flattened the hierarchy in some way, either using nested sets, path enumeration, closure table etc. The table will be maintained using triggers on the category table.

    I would go for (1) unless you are fairly certain that you will have a sustained load of several users per second in the near future. (1 user per second makes 2,5 million visits a month).

    There is nothing wrong with simple code. Complicating code for a speedup that isn’t noticable is wrong.

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

Sidebar

Related Questions

No related questions found

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.