Hello all,
We have some new viewer functions to display multi-level categories. These will be in the next release but I'll show you how to use them now. Note that we haven't added them to the code generator (that's next) so for now it's still an advanced feature. That said, here's how it works.
The functions let you display a multi-level category menu as an indented tree or as a <ul> list (which can work with drop down menu systems). You can also specify a couple formats to display the menu and then "drill down" where clicking on an option shows the "sub options".
Note that currently categories to pages are one to one. Meaning each category maps to one page, not a list of records from another section. There's things you can do to make that work but for this post I'll just be explaining the basics.
Note that I've attached two files: viewer_functions.php and categoryList.php (a sample category list viewer).
Here's how to use this.
1) Make sure you're running v1.20 or newer (upgrade if needed)
2) Create a new "Category" section.
3) Add some record to the category section - some under others.
4) Under Admin > Section Editors > Your Category Section > Viewer Urls - set them both to categoryList.php
5) Either use the attached file (updating the viewer_functions.php path and table name to match your own) or add the following code:
<?php
require_once "C:/wamp/www/sb/core2/admin/lib/viewer_functions.php";
list($categoryRecords, $selectedCategory) = getCategories(array(
'tableName' => 'category',
//'selectedCategoryNum' => '', // defaults to getNumberFromEndOfUrl()
'categoryFormat' => 'showall', // showall, onelevel, twolevel
));
?>
Then use this code to display an html menu:
<!-- category menu -->
<h3>Nested Menu</h3>
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo str_repeat(" ", $categoryRecord['depth']); ?>
<?php if ($categoryRecord['_isSelected']): ?><b><?php endif; ?>
<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif; ?>
<br/>
<?php endforeach; ?>
<!-- /category menu -->
OR - use this code to display the menu in <ul> format:
<!-- unordered list -->
<ul>
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<b><a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a></b>
<?php else: ?>
<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php endif; ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>
<!-- /unordered list -->
And then use this code to display the selected category:
<?php if (!$selectedCategory): ?>
No record selected
<?php endif; ?>
<?php if ($selectedCategory): ?>
<h3><?php echo $selectedCategory['name'] ?></h3>
<?php echo $selectedCategory['content'] ?>
<?php endif; ?>
This is a pretty powerful new feature and makes it very easy to manage web sites where the pages are organized in a hierarchical format.
Give it a try and let me know if you have any questions.
UPDATE: Re-uploaded viewer_functions.php with fixes for issues that Djulia found below.
UPDATE 2: Updated instructions since this code is now included in the latest version.
Dave Edis - Senior Developer
interactivetools.com