display root category name

11 posts by 2 authors in: Forums > CMS Builder
Last Post: August 25, 2011   (RSS)

By rez - August 22, 2011 - edited: August 22, 2011

I have a multi level category menu set up.
At the top of the page, (the same page is details and list), I have:
<?php if ($selectedCategory): ?><?php echo $selectedCategory['name'] ?><?php endif ?>

I would like to change that to always show the root category of the current category (which will sometimes be root or sub category) Something like:

If this is a root category, display its name. If it's a child category, display this child's root.


So no matter where i am in a branch, how do I display the root name with a <?php if ($selectedCategory): ?> statement?

Re: [rez] display root category name

By Jason - August 23, 2011

Hi Rez,

If you want to get the parent name if the selected category has a parent, you can get this by manipulating the breadcrumb field like this:

<?php if ($selectedCategory): ?>

<?php if ($selectedCategory['_hasParent']): // display the parent name?>

<?php
$breadcrumbArray = explode(":", $selectedCategory['breadcrumb']);
echo trim($breadcrumbArray[(count($breadcrumbArray) -2 )]);
?>

<?php else: // display the current categories name?>

<?php echo $selectedCategory['name'] ?>

<?php endif ?>

<?php endif ?>


Note that this code will show the parent name if the selected category has a parent. If the tree is more than 1 level deep, it is not going to be showing the top level category. If you want to display the top level category instead change this:

echo trim($breadcrumbArray[(count($breadcrumbArray) -2 )]);

to this:

echo trim($breadcrumbArray[0]);

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] display root category name

By rez - August 24, 2011 - edited: August 24, 2011

That works great. I realized I want to also display the <?php echo $selectedCategory['content'] ?> of this same category in a different spot on the page. How do a display the content field of it? (note that I added another level, depth 0 ("menu") so for each branch, i will be displaying depth 1. In other words, no matter what panini items are displayed, I will have a title at the top of "sandwiches" and the sandwiches category description.

My nav / categories:
Menu
sandwiches
classic
panini
beverages
coke
coffee


<div id="sub-head" class="grid_16">
<div class="grid_3 prefix_6 suffix_1 alpha"><div id="vert2"><div class="cell"><p><?php if ($selectedCategory): ?>

<?php if ($selectedCategory['_hasParent']): // display the parent name?>

<?php
$breadcrumbArray = explode(":", $selectedCategory['breadcrumb']);
echo trim($breadcrumbArray[1]);
?>

<?php else: // display the current categories name?>

<?php echo $selectedCategory['name'] ?>

<?php endif ?>

<?php endif ?></p></div></div></div><div id="sub-dsc" class="grid_5 suffix_1 omega">
<div id="vert"><div class="cell"><p>

***whatever category is displayed above (currently always a level 1) display it's content field here.*****

</p></div></div>
</div>
</div>



thanks.

Re: [Jason] display root category name

By rez - August 24, 2011 - edited: August 24, 2011

By the way, in this same fashion, I plan to get more complex once I understand, using searches, more depths and more fields from lower depths and knowing exactly where I am in a branch. So the visitor can see lower descriptions and such from lower depths while viewing items in the category they currently have selected.

So hopefully the way i get this working will be the base for that. arrays? or whatever i have to learn like the functions i see _hasParent, the ancestor ones etc.

Thought I'd mention that to make sure i'm heading in the right direction for all of this.

Re: [rez] display root category name

By Jason - August 24, 2011 - edited: August 24, 2011

Hi,

Since we're not able to get the content of the parent record from selectedCategory, a better approach to this would be to use a query to get the parent record directly. If you're using CMSB 2.08 or above, we can use the mysql_get() function like this:

<div id="sub-head" class="grid_16">
<div class="grid_3 prefix_6 suffix_1 alpha"><div id="vert2"><div class="cell"><p><?php if ($selectedCategory): ?>

<?php if ($selectedCategory['_hasParent']): // display the parent name?>

<?php
$parent = mysql_get('categories', $selectedCategory['parentNum']);
$content = $parent['content'];

echo $parent['name'];
?>

<?php else: // display the current categories name?>


<?php
$content = $selectedCategory['content'];
echo $selectedCategory['name']
?>

<?php endif ?>

<?php endif ?></p></div></div></div><div id="sub-dsc" class="grid_5 suffix_1 omega">
<div id="vert"><div class="cell"><p>


<?php echo $content; ?>


</p></div></div>
</div>
</div>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] display root category name

By rez - August 24, 2011

My table is menu_categories but even with that change, I get:
Undefined index: partentNum

Thanks for the help.

Re: [rez] display root category name

By Jason - August 24, 2011

Hi,

Ooops, that was a typo. It should be parentNum. I've made this change in the code as well.

Give that a try.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] display root category name

By rez - August 24, 2011 - edited: August 24, 2011

Was just coming back to say i see that now. I knew what you meant and was missing it.

Thanks for the fresh direction path to learning more possibilities with CMSB. :) Works awesome.

Re: [Jason] display root category name

By rez - August 25, 2011

If i build my whole site's navigation into this category group, making one branch for the food menu (depth 0 as shown above with sandwiches etc), then how would i display a nested ul for just this one branch? (for side navigation on this particular food menu page)

originally, I had:
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<b><?php if (!$categoryRecord['_hasChild']):?><a href="<?php echo $categoryRecord['_link'] ?>"><?php endif; ?><?php echo $categoryRecord['name'] ?><?php if (!$categoryRecord['_hasChild']):?></a><?php endif; ?></b>
<?php else: ?>
<?php if (!$categoryRecord['_hasChild']):?><a href="<?php echo $categoryRecord['_link'] ?>"><?php endif; ?><?php echo $categoryRecord['name'] ?><?php if (!$categoryRecord['_hasChild']):?></a><?php endif; ?>
<?php echo $categoryRecord['_listItemEnd'] ?><?php endif; ?>
<?php endforeach; ?>


that works fine. but now, of course when i add other depth zero branches for home, about us, contact us etc. They are added into this side nav on the food menu page. I tried wrapping parts in <?php if ($categoryRecord ['num']==23): ?> , 23 being the record number for the "menu" category, but couldn't figure out how to get the children to display.

thanks again.