Show current page and parents in multilevel navigation

Hello all,

I am currently building a site that is going to be 3 levels deep. What we want to do is to be able to highlight the current page that the user is on, as well as the parent and grandparent (section) of each link.

There is no CMS or database behind this site, and as such no way to really have no way to know what pages are actually related to which other ones.

To highlight the current page, I’d normally just do thos:


 <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?> href="index.php">Home Page</a></li>

But I am trying to find the best way to find the parent and grandparent page. Am I best to just create 2 variables to target tthe higherarchy (sya $parent and $section), or would I be better to name all my pages as section1-sub1-page.php and use a function that checks the string of the filename for certain characters, and then creates the necessary variables? Or something else?

I think you’ve just answered your own problem. Or at least defined it. You have no way to know what pages are related to other ones. So you need to decide how you’re doing to define that. The ‘best’ way is all the same way - a database table, or nested array/object, are (nearly) the same thing.

Using excessively long pagenames is generally not the right solution, but it is one you can use. (what happens when you’re 6 layers deep? index-food-desserts-chocolate-cake-doublechocolatefun.php ? what a mouthful.

If this feature has not been designed in from the beginning of the project, then you ought to be able to tack on meta-data derived from tables containing the relationships.

main_categories

id - category

1 | Desserts
2 | Starters

sub_categories

id - main_ref - category

1 | 1 | Puddings
2 | 1 | Cakes
3 | 2 | Sea food

items

id - sub_ref - item

1 | 1 | Sticky Toffee Pudding
2 | 2 | Banana Cake
3 | 3 | Prawn Cocktail

You can do similar with single table using natural keys

items

main - sub - item

Desserts - Puddings - Sticky Toffee Pudding
Desserts - Puddings - Jam Rolly Polly

Where you then make sure that all 3 fields enforce uniqueness, and then index the “item” for faster lookups - but you will be matching text to text - so will have to take that into consideration when say, renaming an item.

You could just use and id number of course.

All this will depend on how easily you introduce this to your cms backend - of course.

A mouthful of double chocolate fun? Hmmm! :rofl: