Filtering results from a category menu section

8 posts by 2 authors in: Forums > CMS Builder
Last Post: December 26, 2014   (RSS)

By gkornbluth - December 19, 2014 - edited: December 20, 2014

Hi All,

I’ve created a category menu section and am trying to make a list page that shows only records that match one top level category.

Sorry if I sound frustrated, but I’ve looked at many of the posts related to category menus and have had no luck finding a simple answer.

I must be missing something and hope someone can point me to a solution.

So for example, if there are top level categories Top Level A, Top Level B, Top level C, etc.

And Top Level B has a dozen or so sub categories assigned to it: Top Level B: Sub1, Top Level B: Sub2, Top Level B: Sub3, etc.

How would I create a separate “Top Level B” list page that shows information from only those records that are subs of Top Level B. (And how could I limit the amount of sub levels shown).

Also, if there’s a date field in the category menu section, how would I sort  the returned records by date. (That will probably be obvious, but I’m finding that there’s very little about working with category menu sections that’s obvious)

Thanks,

Jerry Kornbluth

Here’s the basic code I’ve cobbled together to create drop down menus in the viewer for this category menu section (not particularly elegant, but which works as required).
 

<?php
 list($categoryRecords, $selectedCategory) = getCategories(array(
    'tableName'            => 'category', //
    'categoryFormat' => 'showall',  // showall, onelevel, twolevel, breadcrumb
    'defaultCategory' => '', // Enter 'first', a category number, or leave blank '' for none
    'rootCategoryNum' => '0', // Only categories _below_ this one will be shown (defaults to 0 for all)
    'useSeoUrls' => false,
  ));

?>
<?php foreach ($categoryRecords as $categoryRecord): ?>
          <?php echo $categoryRecord['_listItemStart'] ?> <a href="<?php if (!$categoryRecord['url'] == ' ' ):?><?php echo $categoryRecord['_link'] ?><?php else : ?><?php echo $categoryRecord['url'] ?>?<?php echo $categoryRecord['num']?> <?php endif ?>" title="<?php echo $categoryRecord['name'] ?>"><?php echo $categoryRecord['name'] ?></a> <?php echo $categoryRecord['_listItemEnd'] ?>
          <?php endforeach ?>
        </ul>

And here’s what I tried as a simple test for the list page, but it obviously show all records regardless of their level and needs a where or if statement.

<?php list($categoryRecords, $categoryMetaData) = getRecords(array(
    'tableName'   => 'category',
  ));
  ?>
 
<?php foreach ($categoryRecords as $record): ?>
<?php echo ($record['name']) ?><br>
<?php echo ($record['content']) ?>
<hr>
<?php endforeach ?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Jerry -

Depending on the circumstances, I use one of two methods:  If I don't "know" the top level number at coding time, I'll read all of my records in (I need this for the full menu system anyway) and then use a foreach that looks at parentNum vs some other variable that I may set (maybe the current record's num) - if the records in the category list have parentNum == this_records_num, then that's what I'd use - that's how I get around this and what I use when I have a side-bar menu that's only for "this category".  I find that looping through code is faster than re-reading from the database multiple times.

If you know the parent you are working from, then you could use rootCategoryNum and set to the level you want, then use categoryFormat => onelevel (or however many levels you need) to only get the rows below rootCategoryNum you need.  This is what I do when I have both a top and left side navigation as I use a single table called Main Navigation that houses top nav entries, left nav entries and non-navigational entries, allowing me to have a single driver script for all pages.

Hope this helps.

Gary.

Hi Gary,

Thanks for looking at this.

I'm afraid that category menus are still a mystery to me, and I'm not sure I understand your suggestion, even after mucking about a bit.

Let's say I had 3 top level categories "Services", "Links" and "Projects", and I want to create a list page with information from only those records that are in the top level category "Services"

Could I impose enough to ask you to suggest how I would modify the code above to accomplish that in your 2 scenarios?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By garyhoffmann - December 23, 2014 - edited: December 23, 2014

Sure, no problem.

Assuming "services" has a record "num" of 2, for example, set 'rootCategoryNum' => '2',

in your getCategories options

Then, depending on the depth you want (any levels below 2 would be alllevels, only the level directly below entry 2 would be onelevel, etc.)

<?php
 list($categoryRecords, $selectedCategory) = getCategories(array(
    'tableName'            => 'category', //
    'categoryFormat' => 'showall',  // showall, onelevel, twolevel, breadcrumb
    'defaultCategory' => '', // Enter 'first', a category number, or leave blank '' for none
    'rootCategoryNum' => '2', // Only categories _below_ this one will be shown (defaults to 0 for all)
    'useSeoUrls' => false,
  ));

?>
<?php foreach ($categoryRecords as $categoryRecord): ?>
          <?php echo $categoryRecord['_listItemStart'] ?> <a href="<?php if (!$categoryRecord['url'] == ' ' ):?><?php echo $categoryRecord['_link'] ?><?php else : ?><?php echo $categoryRecord['url'] ?>?<?php echo $categoryRecord['num']?> <?php endif ?>" title="<?php echo $categoryRecord['name'] ?>"><?php echo $categoryRecord['name'] ?></a> <?php echo $categoryRecord['_listItemEnd'] ?>
          <?php endforeach ?>
        </ul>

For example, for my top navigation items in my main nav, I use:

            list($tnavRecords, $selectedCategory) = $cmsbo->getCategories(array(
                'tableName'           => $main_nav_name,
                'selectedCategoryNum' => $selectedCatNum,
                'categoryFormat'      => 'showall',  // showall, onelevel, twolevel
                'rootCategoryNum'     => $rootTop,
            ));

(ignore the fact that I have created an OO implementation of CMSB for a moment)

By gkornbluth - December 23, 2014 - edited: December 23, 2014

Thanks Gary,

I'm sure this will help sort things out.

It's getting pretty late here so I'll have a look in the morning.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By gkornbluth - December 26, 2014 - edited: December 26, 2014

Hi Gary,

Sorry it took so long to get back to you.

Your input was a great help.

I ended up using  a combination of standard sections and a category section in the nav menu.

I my situation I had a "links" category and didn't want to show the category level 2 links for them on the nav menu, but want instead to list them on a separate list page.

I'm sure it can be done, but I was against a deadline and needed to get the job finished.

I'd like to know how to do it for the next time though.

Best,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Just realized there is no private message capability in the forum...you can send me a message at gary at techanalysts dot com