Category menu?
8 posts by 2 authors in: Forums > CMS Builder
Last Post: October 19, 2015 (RSS)
By Toledoh - October 7, 2015
Hi Guys,
I've got a category section that I'm trying to get to produce
<ul id="sidenavigation">
<li><a data-toggle="collapse" href="#child" aria-expanded="false" aria-controls="child">Link</a>
<ul id="child" class="collapse">
<li><a href="">Child</a>
<li><a href="">Child</a>
<li><a href="">Child</a>
</ul>
</li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
</ul>
I'm using:
// load STUCTURE
list($categories, $selectedCategory) = getCategories(array(
'tableName' => 'structure',
'categoryFormat' => 'twolevel',
'rootCategoryNum' => '1',
'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
));function customMenuUlAttr($category) {
return ($category['_hasParent'] && $category['depth'] == '1')? " id=\"child\" class=\"collapse\"" : "" ;
}function customMenuLiAttr($category) {
return ($category['_hasChild'] && $category['depth'] == '0')? 'data-toggle="collapse" href="#child"': "" ;
}...
<ul id="sidenavigation">
<?php foreach ($categories as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?><?php if ($categoryRecord['_hasChild']): ?>
<a><?php echo $categoryRecord['name'] ?></a>
<?php else: ?>
<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>
</div>
It's close, but not right. It's returning the data-toggle="collapse" href="#child" to the LI rather than the A
I also need to somehow get the #child to use the record number of the parent like #child<?php echo $categoryRecord['name'] ?> so if there's multiple accordions they are recognised independently.
Example is here: http://182.160.160.10/~mcmahonclarkecom/structure.php?Funds-Management-Experience-6
Tim (toledoh.com.au)
By gregThomas - October 12, 2015 - edited: October 13, 2015
Hey Tim,
If you're looking to add #child to the href of the items with children, something like this should work:-
// load STUCTURE
list($categories, $selectedCategory) = getCategories(array(
'tableName' => 'structure',
'categoryFormat' => 'twolevel',
'rootCategoryNum' => '1',
'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
//'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
));
function customMenuUlAttr($category) {
return ($category['_hasParent'] && $category['depth'] == '1')? " id=\"child\" class=\"collapse\"" : "" ;
}
//WE NO LONGER NEED THIS, SO I'VE COMMENTED IT OUT
/*
function customMenuLiAttr($category) {
return ($category['_hasChild'] && $category['depth'] == '0')? 'data-toggle="collapse" href="#child"': "" ;
}*/
<div>
<ul id="sidenavigation">
<?php foreach ($categories as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<!-- if we're dealing with
<?php if ($categoryRecord['_hasChild']): ?>
<?php $parentNum = $categoryRecord['num']; ?>
<a><?php echo $categoryRecord['name'] ?></a>
<?php else: ?>
<a href="#child<?php echo @$parentNum; ?>"><?php echo $categoryRecord['name'] ?></a>
<?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>
</div>
This is example code, so you might have to make a few changes to get it working. So if the item has a child, we get its num value, and set it to $parentNum. Then if the items is child item, we set the href as #child with the parentNum value as the link.
Let me know if you have any questions.
Cheers,
Greg
PHP Programmer - interactivetools.com
By Toledoh - October 12, 2015
Hey Greg,
That's certainly closer, but I need to edit the ul tag created for the children, by applying the parents #ID and the class. Would it be some additional 'ulAttributesCallback' but only applied if it has a parent?
<ul id="sidenavigation">
<li><a data-toggle="collapse" href="#child" aria-expanded="false" aria-controls="child">Link</a>
<ul id="child" class="collapse">
<li><a href="">Child</a>
<li><a href="">Child</a>
<li><a href="">Child</a>
</ul>
</li>
<li><a href="">Link</a></li>
<li><a href="">Link</a></li>
</ul>
Tim (toledoh.com.au)
Hey Tim,
I think something like this will work:
<?php
// load STUCTURE
list($categories, $selectedCategory) = getCategories(array(
'tableName' => 'structure',
'categoryFormat' => 'twolevel',
'rootCategoryNum' => '1',
'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
//'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
));
function customMenuUlAttr($category) {
global $GLOBALS;
return ($category['_hasParent'] && $category['depth'] == '1')? " id=\"child{$GLOBALS['PARENT_NUM']}\" class=\"collapse\"" : "" ;
}
//WE NO LONGER NEED THIS, SO I'VE COMMENTED IT OUT
/*
function customMenuLiAttr($category) {
return ($category['_hasChild'] && $category['depth'] == '0')? 'data-toggle="collapse" href="#child"': "" ;
}*/
?>
<div>
<ul id="sidenavigation">
<?php foreach ($categories as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<!-- if we're dealing with
<?php if ($categoryRecord['_hasChild']): ?>
<?php $GLOBALS['PARENT_NUM'] = $categoryRecord['num']; ?>
<a><?php echo $categoryRecord['name'] ?></a>
<?php else: ?>
<a href="#child<?php echo @$GLOBALS['PARENT_NUM']; ?>"><?php echo $categoryRecord['name'] ?></a>
<?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>
</div>
So if an item has a child, we set the num value to a global called $GLOBALS['PARENT_NUM']. We can then detect the value in any function we use.
I've then modified the UL tag function so that it will have the relevant parent num added to the child tag, by adding the parent num value where required.
Cheers,
Greg
PHP Programmer - interactivetools.com
By Toledoh - October 13, 2015
Thanks Greg - but I'm getting an error: Notice: Undefined index: PARENT_NUM in /home/mcmahonclarkecom/public_html/structure.php on line 31 which relates to
return ($category['_hasParent'] && $category['depth'] == '1')? " id=\"child{$GLOBALS['PARENT_NUM']}\" class=\"collapse\"" : "" ;
Tim (toledoh.com.au)
Hey Tim,
I'm not sure what's causing this, please could you fill out a second level support request:
https://www.interactivetools.com/support/email_support_form.php
Then I can add some debugging code to work out what the issue could be.
Thanks!
Greg
PHP Programmer - interactivetools.com
Hey Tim,
Here is the final code to make a menu using bootstrap collapse (http://getbootstrap.com/javascript/#collapse) :-
// load STUCTURE
list($categories, $selectedCategory) = getCategories(array(
'tableName' => 'structure',
'categoryFormat' => 'twolevel',
'rootCategoryNum' => '1',
'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
//'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
));
function customMenuUlAttr($category) {
global $GLOBALS;
return ($category['_hasParent'] && $category['depth'] == '1')? " id=\"child{$category['parentNum']}\" class=\"collapse\"" : "" ;
}
So first we add the parent id and class to the UL tag if we're dealing with a child item on the second level.
<ul id="sidenavigation">
<?php foreach ($categories as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_hasChild']): ?>
<a aria-expanded="false" data-toggle="collapse" href="#child<?php echo @$categoryRecord['num']; ?>" ><?php echo $categoryRecord['name'] ?></a>
<?php else: ?>
<a href="<?php echo $categoryRecord['_link']; ?>" ><?php echo $categoryRecord['name'] ?></a>
<?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>
The above code checks if we're dealing with a parent item that has a child, and if it does adds the appropriate classes to make the link a menu collapse type.
Thanks,
Greg
PHP Programmer - interactivetools.com