How to generate nav menu for only particular part of a Category menu tree branch?
2 posts by 2 authors in: Forums > CMS Builder
Last Post: June 27, 2012 (RSS)
By davidcmsb - June 26, 2012 - edited: June 26, 2012
Hi,
I need to know how to have a vertical navigation menu on the public website show only a particular portion of a category menu tree from a certain point on one branch through to the ending last child menu item.
Let's say my categories in CMSB are set up with this hierarchy:
-Food
-Drinks
--Sodas
---Coke
---Pepsi
-Booze
--Mixed Drinks
---Screwdriver
---White Russian
I want to be able to have a vertical navigation menu appear on the public website that would show a particular branch starting from a particular point through to the end. For example, a menu that shows just the Mixed Drinks menu items, i.e. --
--Mixed Drinks
---Screwdriver
---White Russian
-- OR a menu that shows just the Drinks menu items, i.e. --
-Drinks
--Sodas
---Coke
---Pepsi
After studying the fields generated by CMSB for Categories, I think I figured out the logic needed to do this, but don't know exactly how to code it in PHP.
To do what I want to do, it seems that the lineage field is the key.
Looking at the entries in the lineage field, I see that all category items in the same branch, starting from a particular point in the branch, include the same category num in the lineage field and that is the category num for the starting point in the branch I want to select.
So I need to have the code run a foreach loop and generate the vertical navigation menu only for category items whose lineage field contains and includes (not equals) a given category num, i.e., :2:, so it would pick up all category items whose lineage field includes that category num for the category where I want the menu to start being output from
In English (as opposed to PHP), it seems that the logic here would be:
• Run foreach loop, but only on Category records whose lineage field includes, e.g., :2: and echo for these categories the link and label.
But I do not know the exact specific code in PHP language needed to do this.
I use the following PHP code to generate the the vertical navigation for all category items on the public web pages--
How do I modify this to do what I want to do here and show a menu only for a selected branch starting at a given point I select in the branch? What is the specific PHP code I should use?
I was thinking, in logic terms, something like this, but need to know the exact PHP code for it. Shown below is the code above, modified to show the logic I think is needed, using English (not PHP) in Capital letters:
I'm also wondering if I'd need to wrap the whole thing inside of some conditional if statement, maybe something like:
Another permutation is that perhaps if it is wrapped inside an outer if statement, then I wouldn't need the inner if statement??? I have no idea. I'm not that fluent in PHP, but am in English.
So I thought I'd write out here in English what I've figured out the logic to be (i.e., run the foreach and listitemstart/end routine that generates the vert. nav menu but only on category records whose lineage field contains the category num I pick, so it generates a menu starting with that category and going through to all of the end children in that branch and its sub-branches), and ask if you think that's the correct logic to do what I want, and if so, what is the PHP code I should use.
So,
(A) do you think that using the lineage field like this to identify the category items I want in the menu that is output is the correct way to go?
and,
(B) If so, what's the exact PHP code I'd need to do what I'm trying to do. I want to do this in the body, not in the initial call in the head.
Thanks very much,
David
I need to know how to have a vertical navigation menu on the public website show only a particular portion of a category menu tree from a certain point on one branch through to the ending last child menu item.
Let's say my categories in CMSB are set up with this hierarchy:
-Food
-Drinks
--Sodas
---Coke
---Pepsi
-Booze
--Mixed Drinks
---Screwdriver
---White Russian
I want to be able to have a vertical navigation menu appear on the public website that would show a particular branch starting from a particular point through to the end. For example, a menu that shows just the Mixed Drinks menu items, i.e. --
--Mixed Drinks
---Screwdriver
---White Russian
-- OR a menu that shows just the Drinks menu items, i.e. --
-Drinks
--Sodas
---Coke
---Pepsi
After studying the fields generated by CMSB for Categories, I think I figured out the logic needed to do this, but don't know exactly how to code it in PHP.
To do what I want to do, it seems that the lineage field is the key.
Looking at the entries in the lineage field, I see that all category items in the same branch, starting from a particular point in the branch, include the same category num in the lineage field and that is the category num for the starting point in the branch I want to select.
So I need to have the code run a foreach loop and generate the vertical navigation menu only for category items whose lineage field contains and includes (not equals) a given category num, i.e., :2:, so it would pick up all category items whose lineage field includes that category num for the category where I want the menu to start being output from
In English (as opposed to PHP), it seems that the logic here would be:
• Run foreach loop, but only on Category records whose lineage field includes, e.g., :2: and echo for these categories the link and label.
But I do not know the exact specific code in PHP language needed to do this.
I use the following PHP code to generate the the vertical navigation for all category items on the public web pages--
<?php foreach ($categoriesRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach ?>
How do I modify this to do what I want to do here and show a menu only for a selected branch starting at a given point I select in the branch? What is the specific PHP code I should use?
I was thinking, in logic terms, something like this, but need to know the exact PHP code for it. Shown below is the code above, modified to show the logic I think is needed, using English (not PHP) in Capital letters:
<?php foreach ($categoriesRecords as $categoryRecord): ?>
• IF A RECORD'S LINEAGE FIELD CONTAINS AND INCLUDES :2:, THEN DO THIS ONLY FOR EACH ONE THAT DOES HAVE :2: IN ITS LINEAGE FIELD:
<?php echo $categoryRecord['_listItemStart'] ?>
<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php echo $categoryRecord['_listItemEnd'] ?>
• END THE IF
<?php endforeach ?>
I'm also wondering if I'd need to wrap the whole thing inside of some conditional if statement, maybe something like:
• IF A RECORD'S LINEAGE FIELD CONTAINS AND INCLUDES :2:, THEN DO THIS ONLY FOR EACH ONE THAT DOES HAVE :2: IN ITS LINEAGE FIELD:
<?php foreach ($categoriesRecords as $categoryRecord): ?>
• IF A RECORD'S LINEAGE FIELD CONTAINS AND INCLUDES :2:, THEN DO THIS ONLY FOR EACH ONE THAT DOES HAVE :2: IN ITS LINEAGE FIELD:
<?php echo $categoryRecord['_listItemStart'] ?>
<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php echo $categoryRecord['_listItemEnd'] ?>
• END THE IF
<?php endforeach ?>
• END THE IF
Another permutation is that perhaps if it is wrapped inside an outer if statement, then I wouldn't need the inner if statement??? I have no idea. I'm not that fluent in PHP, but am in English.
So I thought I'd write out here in English what I've figured out the logic to be (i.e., run the foreach and listitemstart/end routine that generates the vert. nav menu but only on category records whose lineage field contains the category num I pick, so it generates a menu starting with that category and going through to all of the end children in that branch and its sub-branches), and ask if you think that's the correct logic to do what I want, and if so, what is the PHP code I should use.
So,
(A) do you think that using the lineage field like this to identify the category items I want in the menu that is output is the correct way to go?
and,
(B) If so, what's the exact PHP code I'd need to do what I'm trying to do. I want to do this in the body, not in the initial call in the head.
Thanks very much,
David
Re: [davidcmsb] How to generate nav menu for only particular part of a Category menu tree branch?
By Jason - June 27, 2012
Hi David,
An easier way to do this would be to use getCategories to return all the children of the selected category. When there is a record number at the end of your url, getCategories will attempt to return $selectedRecord, which is the record that matches the number in the url. With this, we can perform a second getCategories call like this:
You can then output it like this:
Hope this helps get you started
An easier way to do this would be to use getCategories to return all the children of the selected category. When there is a record number at the end of your url, getCategories will attempt to return $selectedRecord, which is the record that matches the number in the url. With this, we can perform a second getCategories call like this:
$childCategories = array();
if ($selectedCategory) {
list($childCategories, ) = getCategories(array(
'tableName' => 'category',
'rootCategoryNum' => intval($selectedCategory['num']),
));
}
You can then output it like this:
<?php if ($selectedCategory): ?>
<ul>
<li><?php echo $selectedCategory['name'];?></li>
<ul>
<?php endif ?>
<?php foreach ($childCategories as $category): ?>
<?php echo $category['_listItemStart'];?>
<?php echo $category['name']."-".$category['num'];?>
<?php echo $category['_listItemEnd'];?>
<?php endforeach ?>
<?php if ($selectedCategory): ?>
</ul>
</ul>
<?php endif ?>
Hope this helps get you started
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/