Category Menu - parent/child question

3 posts by 2 authors in: Forums > CMS Builder
Last Post: March 23, 2018   (RSS)

By mizrahi - March 21, 2018

I am using a category menu editor (research_item_categories) to tag articles in the another multi-record editor (research_documents). The category menu is limited to just 2 levels, parents and one level of children. The parents are not being used for tags, only for organizing the tags into different groupings.

I have been able to successfully setup a child page to show the proper articles. 

But for a parent page, i want to show all articles tagged for any of it's children.

Here is my working code, bold/red text is where I believe I need help.

  // load records from 'research_item_categories'
  list($research_item_categoriesRecords, $selectedResearch_item_categories) = getCategories(array(
    'tableName'            => 'research_item_categories', //
    'categoryFormat'       => 'onelevel',  // showall, onelevel, twolevel, breadcrumb
    'defaultCategory'      => '',    // Enter 'first', a category number, or leave blank '' for none
    
    // advanced options (you can safely ignore these)
    'rootCategoryNum'      => '',      // Only categories _below_ this one will be shown (defaults to blank or 0 for all)
    'ulAttributes'         => '',      // add html attributes to <ul> tags, eg: 'class="menuUL"' would output <ul class="menuUL">
    'selectedCategoryNum'  => '',      // this record number is returned as the "selected category", defaults to getLastNumberInUrl()
    'ulAttributesCallback' => '',      // ADVANCED: custom function to return ul attributes, eg: 'myUlAttr' and function myUlAttr($category) { return "id='ul_uniqueId_{$category['num']}'"; }
    'liAttributesCallback' => '',      // ADVANCED: custom function to return li attributes, eg: 'myLiAttr' and function myLiAttr($category) { return "id='li_uniqueId_{$category['num']}'"; }
    'loadCreatedBy'        => false,   // loads createdBy.* fields for user who created category record (false is faster)
    'loadUploads'          => true,    // loads upload fields, eg: $category['photos'] gets defined with array of uploads (false is faster)
    'ignoreHidden'         => false,   // false = hide records with 'hidden' flag set, true = ignore status of hidden flag when loading records
    'debugSql'             => false,   // display the MySQL query being used to load records (for debugging)
  ));


    if($selectedResearch_item_categories['depth'] > 0) {
        list($research_documentsRecords, $research_documentsMetaData) = getRecords(array(
            'tableName'   => 'research_documents',
            'loadUploads' => true,
            'allowSearch' => false,
            'where' => "category = ".$selectedResearch_item_categories['num'],
        ));
    } else {
        list($research_documentsRecords, $research_documentsMetaData) = getRecords(array(
            'tableName'   => 'research_documents',
            'loadUploads' => true,
            'allowSearch' => false,
            // THIS IS WHERE I NEED THE HELP
            // 'where' => "category = ".$selectedResearch_item_categories['num'],
        ));
    };

By Dave - March 23, 2018

Hi mizrahi, 

What about this after getCategories: 

  // get child nums for selected record
  $childNumsByParentNum = [];
  foreach ($categoryRecords as $record) {
    $childNumsByParentNum[ $record['parentNum'] ][] = $record['num'];
  }
  $childNumsAsCSV = mysql_escapeCSV( $childNumsByParentNum[ $selectedCategory['num'] ]);

And then

'where' => " category IN ($childNumsAsCSV) ",

Let me know if that works for you.

Dave Edis - Senior Developer
interactivetools.com