Two category sections on the same page using getCategories.

7 posts by 2 authors in: Forums > CMS Builder
Last Post: September 14, 2013   (RSS)

By aev - September 9, 2013

Hi,

I'm trying to use two category sections on the same page, making the active one display as "onelevel" and the other to only list it's top-level.

Here is my code so far:

if ($getSection == "nav") {

  // nav1
  list($categoryRecords, $selectedCategory) = getCategories(array( 
  'tableName' => 'nav', 
  'defaultCategory' => 'first', // Enter 'first', a category number, or leave blank '' for none
  'categoryFormat' => 'onelevel', // showall, onelevel, twolevel 
  'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
  'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
  'selectedCategoryNum' => mysql_escapef(@$getRecord), 
  ));

  // nav2
  list($categoryRecords2) = getCategories(array( 
  'tableName' => 'nav2', 
  'defaultCategory' => '', // Enter 'first', a category number, or leave blank '' for none
  'categoryFormat' => 'onelevel', // showall, onelevel, twolevel 
  'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
  'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
  ));

} else {

  ..the same as above but swap 'nav' and 'nav2'

}

The problem is that the second category menu get's selected in the same way as the primary, showing like a selected 'onelevel' menu. Any suggestions?

-aev-

By Jason - September 9, 2013

Hi,

How does the second record set ($categoryRecords2) show as "selected"?  Is this a function of your callback functions, customMenuUlAttr, and customMenuLiAttr?  If so, you can update those functions to look at the _tableName attribute of each record so that you don't style records from nav2 the same way.

Also, if you want to only show top level categories, you can look at the depth, field.  top level categories will have a depth of 0, so you can filter them like this:

<?php foreach ($categoryRecords2 as $category): ?>
  <?php if ($category['depth'] != 0) { continue; } ?>

Hope this helps,

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By aev - September 9, 2013

Hi,

when a $categoryRecords record is loaded I also get a "[_isSelected] => 1" on the record with the same record-num in $categoryRecords2 even if I load $categoryRecords2 with "categoryFormat => onelevel" and no "selectedCategoryNum" set.

As you suggest I could workaround this by changing my code for displaying $categoryRecords2, but I would rather know if I can adjust getCategories and keep the same code for displaying my categories.

Is there any documentation explaining all the details for using getCategories?

-aev-

By Jason - September 9, 2013

Hi,

By default, _isSelected is set by looking at the last number in the URL string.  You can change this by setting the 'selctedCategoryNum' option in your getCategories call.  If you set it like this:

'selectedCategoryNum' => '0',

then no record in that record set will have _isSelected set, as there will never be a record number 0.  It's important to remember to put the single quotes around the 0, otherwise this solution won't work.

We have two good tutorials in the forum to give you a good start on using getCategories:

http://www.interactivetools.com/forum/forum-posts.php?postNum=2192758#post2192758

http://www.interactivetools.com/forum/forum-posts.php?postNum=2201307#post2201307

Please let me know if you have any further questions.

Thanks!

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By aev - September 9, 2013

HI,

thanks! 'selectedCategoryNum' => '0', was the missing piece I needed for getting this to work!

Here is the code I ended up using:

if ($getSection == "nav") {

  // Menu 1 (selected)
  list($categoryRecords, $selectedCategory) = getCategories(array( 
    'tableName' => 'nav', 
    'defaultCategory' => 'first', // Enter 'first', a category number, or leave blank '' for none
    'categoryFormat' => 'onelevel', // showall, onelevel, twolevel 
    'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
    'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
    'selectedCategoryNum' => mysql_escapef(@$getRecord), 
  ));

  // Menu 2
  list($categoryRecords2) = getCategories(array( 
    'tableName' => 'nav2', 
    'defaultCategory' => '', // Enter 'first', a category number, or leave blank '' for none
    'categoryFormat' => 'onelevel', // showall, onelevel, twolevel 
    'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
    'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
    'selectedCategoryNum' => '0'
  ));

} elseif  ($getSection == "nav2") {

  // Menu 1
  list($categoryRecords) = getCategories(array( 
    'tableName' => 'nav',
    'defaultCategory' => '', // Enter 'first', a category number, or leave blank '' for none
    'categoryFormat' => 'onelevel', // showall, onelevel, twolevel 
    'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
    'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
    'selectedCategoryNum' => '0' 
  ));

  // Menu 2 (selected)
  list($categoryRecords2, $selectedCategory ) = getCategories(array( 
    'tableName' => 'nav2', 
    'defaultCategory' => 'first', // Enter 'first', a category number, or leave blank '' for none
    'categoryFormat' => 'onelevel', // showall, onelevel, twolevel 
    'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
    'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
    'selectedCategoryNum' => mysql_escapef(@$getRecord), 
  ));

}

-aev-

By Jason - September 14, 2013

Hi,

If you use selectedCategoryNum, then getCategories will not take 'defaultCategory' into account.  In addition, defaultCategory will only be used if getLastNumberInUrl() doesn't return anything, which in your case it will return 4.

How are you assigning a value to $getRecord?  The best way to accomplish what you're looking for is to set the selectedCategoryNum value to the record you are looking for..  So, if you want the record num of the first top level category, you can do something like this:

// get first category num
$firstTopLevelCategory = mysql_get($getNav, null, "depth = '0' ORDER BY globalOrder ASC");
$firstCategoryNum      = @$firstTopLevelCategory['num'];

list($categoryRecords, $selectedCategory) = getCategories(array( 
  'tableName'            => mysql_escapef(@$getNav), 
  'defaultCategory'      => 'first', // Enter 'first', a category number, or leave blank '' for none
  'categoryFormat'       => 'onelevel', // showall, onelevel, twolevel 
  'ulAttributesCallback' => 'customMenuUlAttr', // ADVANCED: custom function to return ul attributes, eg: myUlAttr($category);
  'liAttributesCallback' => 'customMenuLiAttr', // ADVANCED: custom function to return li attributes, eg: myLiAttr($category);
  'selectedCategoryNum'  => mysql_escapef($firstCategoryNum), 
));

Hope this helps

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/