I would like to be able to create breadcrumbs for a content page, however, the categories in which some of the content is located can have unlimited depth, so I'm not sure how to do this without getting each category one at a time, and then getting its parent, etc. d. It seems like this might be an easier way, but I can't figure it out.
I have an article table
article_id article_name article_cat_id
I also have a category table
cat_id cat_name cat_parent
Cat parent is an identifier of another category, the category of which is a child.
Imagine an article consisting of 5 categories, as far as I can tell, I would need to build breadcrumbs like this (for example, the code must explicitly 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']; } ?>
That would give me
array( 'Category 1', 'Category 2', 'Category 3', 'Category 4', 'Category 5', 'Content Item' )
What can I use for my breadcrumbs, but for this I needed 5 queries, which is not very desirable.
Can anyone suggest any better solutions?