Problem getting top level category id for sub item

Get help with installation and running phpwcms here. Please do not post bug reports or feature requests here.
Post Reply
whizkid
Posts: 14
Joined: Wed 1. Dec 2004, 11:48

Problem getting top level category id for sub item

Post by whizkid »

Background is: I want to use NAV_TABLE_COLUMN:x dynamically on the left to display the menu for the top level category, selected from a breadcrumb menu.

I have read several postings here about getting the current category id using $GLOBALS['content']['cat_id']; The problem is, it always returns the category of the actual item in the site structure.

For example:
1 (top level, cat_id=1)
1.1 (sub level, cat_id=3)
1.1.1 (sub sub level, cat_id=5)
2 (top level, cat_id=2)
2.1 (sub level, cat_id=4)

For 1 and 2 it returns the correct value, but for 1.1 and 2.1 it returns 3 or 4, but I need the top level (1 or 2).

I hope somebody can understand what I mean. I always need to get the top level ID to display the correct submenu.

Any help is greatly appreciated.
User avatar
nekket
Posts: 613
Joined: Tue 18. Nov 2003, 15:46
Location: Baden-Baden
Contact:

Post by nekket »

same problem here .. :x
whizkid
Posts: 14
Joined: Wed 1. Dec 2004, 11:48

Post by whizkid »

Solved it yesterday... here is the result:

create a file sample_ext_php/dyn-nav.php with the following content:

---
<?php
// used to return the code for menu based on given ID
// always returns top level id, can be used to use only 1 template for all

// get the current level ID (layout item id)
$check_id = $GLOBALS['content']['cat_id'];

// get the parent structure id
$struct_id = $GLOBALS['content']['struct'][$check_id]['acat_struct'];

// init nav_id with category id
$nav_id = $check_id;

while ($struct_id != 0) {
// save last nav_id
$nav_id = $struct_id;
// get parent of struct_id
$struct_id = $GLOBALS['content']['struct'][$struct_id]['acat_struct'];
}

// output replacement tag
echo '{NAV_TABLE_COLUMN:' . $nav_id . '}';
?>
---

Then in the template include the replacement tag:
{PHP:sample_ext_php/dyn-nav.php}


Works like a charm :)

-whiz
Post Reply