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