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 13, 2013

Hi,

I have a new problem similar to the one above.

On a another site I'm working on we have 4 category sections, but we only display one at a time. I load the pages with this url:

http://www.example.com/index.php?nav=2

And pass the nav value like this:

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(@$getRecord), 
));

This work fine, the problem is that since this page doesn't have the url variable like id=34 I use to pass to $getRecord, this getCategories call treats my nav=2 as the record number. We don't want this, we would like the category to load with the 'first' cat selected.

To sum this up; how can we load a category with the first category selected, not cat. num=1 but categoryRecords[0] (this can be cat. num=3).

-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/