Multi-level Category Functions

63 posts by 17 authors in: Forums > CMS Builder
Last Post: December 19, 2011   (RSS)

By Dave - July 31, 2008 - edited: November 15, 2010

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("&nbsp; &nbsp; &nbsp;", $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
Attachments:

categorylist_001.php 3K

Re: [Dave] Multi-level Category Functions

Hi Dave,
This sounds brilliant and is probably what I am looking for. Are you able to provide a couple of images of the actual page with the new menu? I haven't yet got my CMSB anywhere near production but am keen to know if this will work for my sites.

Mickey

Re: [Dave] Multi-level Category Functions

By Djulia - August 1, 2008 - edited: August 1, 2008

Hi Dave,

1) I have an error message with showall :

Strict Standards: Only variables should be assigned by reference in /../lib/viewer_functions.php on line 463
Strict Standards: Only variables should be assigned by reference in /../lib/viewer_functions.php on line 464


2) I also have an error message with the use of the SeoUrls option :

Category Viewer (category) errors
Unknown option 'useSeoUrls' specified
Valid option names are: (...)



You have an idea ?

Thank you for your assistance.

Djulia


P.S.: for Mickey (image-level.gif)

Re: [Djulia] Multi-level Category Functions

Thanks Djulia,
Hopefully Dave can sort your problem out soon.

Mickey

Re: [Djulia] Multi-level Category Functions

By Dave - August 1, 2008

Hi Djulia,

Can you attach the viewer file you're using?

And what version of PHP are you using?

Thanks!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Multi-level Category Functions

By Djulia - August 1, 2008 - edited: August 1, 2008

I use the file which you propose : categoryList.php.

My version php is: PHP Version 5.2.6

You have an idea ?

Thanks,

Djulia


P.s.: I made a test with version 6 of PHP, but I have an error message (I use the file htaccess by default) :[font "Times New Roman"]

Fatal error: Call to undefined function get_magic_quotes_gpc() in /../lib/init.php on line 72
Attachments:

categorylist_002.php 3K

Re: [Dave] Multi-level Category Functions

By Djulia - August 1, 2008

Thank you Dave,
The errors are corrected for me. [:)]

I have an observation.

My menu is structured as follows :

Level 1
---Level 2
------Level 3

...and I use the SeoUrls option.

The URL for Level 2 and Level 3 turns over me :
categoryList.php/Level-1-Level-2-Level-3/

It will be possible, with the final version, to obtain:
categoryList.php/Level-1/Level-2/Level-3/

I also wonder about the "id" which accompanies the URL.
It will be possible to remove it ?

Thanks for the feedback.

Djulia

Re: [Djulia] Multi-level Category Functions

By Dave - August 1, 2008

Hi Djulia,

You need to use absolute urls when using SEO urls. Try setting the Viewer Urls under "Section editors" to /categoryList.php or whatever the path from your website root is. That should fix the Level-1-Level-2-Level-3 problem.

Next, you can remove id, but it requires custom php code. For simplicity the viewers look for the record number on the end of the url. So if you had a field for 'filename' say you'd need to parse that out of the url, look up the category with the matching filename, and then set it's record number in the 'selectedCategoryNum' option. You need something unique to match again to know which record or category to load or show as selected.

It's basically the same process as removing the 'id' from any of the viewers.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Multi-level Category Functions

By Djulia - August 1, 2008 - edited: August 1, 2008

Thank you Dave,

I already tested your proposal, but that does not function.

Even with option SEO on false, I obtain :

for Level-1
/categoryList.php?Level-1

for Level-2
/categoryList.php?Level-1-Level-2

and for Level-3
/categoryList.php?Level-1-Level-2-Level-3

"/" is automatically replaced by "-".

Another idea ?


Thanks,

Djulia