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
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 rez - August 22, 2011
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php if($categoryRecord['_isSelectedBranch' ] && $categoryRecord['depth'] == 0): ?>
<?php echo $categoryRecord['name'] ?>
<?php endif ?><?php endforeach; ?>
Ok, is there a shortcut for this to get straight there with just some type of, "what is this selected branches root?" Looping through everything in my DB just to get to one thing 10 different places on my page couldn't possibly be a good thing.
Re: [rez] display root category name
By Jason - August 23, 2011
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
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
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
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
Undefined index: partentNum
Thanks for the help.
Re: [rez] display root category name
By Jason - August 24, 2011
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
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
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.