Multiple Category Issue

6 posts by 2 authors in: Forums > CMS Builder
Last Post: March 7, 2011   (RSS)

By Perchpole - March 1, 2011

I currently use the following set-up to determine which content appears on my category pages. It filters the records according to the selected branch of the category tree.

//categories
$selectedBranchNums = '0';
foreach ($categoryRecords as $category) {
if ($category['_isSelected'] || $category['_isAncestorSelected']) {
$selectedBranchNums .= ',' . (int) $category['num'];
}
}

// content
list($contentRecords, $contentMetaData) = getRecords(array(
'tableName' => 'content',
'where' => " category IN ($selectedBranchNums) ",
));


This works very well. The user assigns each record to a category at the time of creation which is then listed on the corresponding category page.

Just one issue - you can only assign each record to a single category.

How can I amend the code above (retaining the branch functionality) so that records can be assigned to multiple categories?

:0)

Perchpole

Re: [Perchpole] Multiple Category Issue

By Jason - March 3, 2011

Hi Perchpole,

Try this:

//categories
$selectedBranchNums = '0';
foreach ($categoryRecords as $category) {
if ($category['_isSelected'] || $category['_isAncestorSelected']) {
$selectedBranchNums .= ',' . join( ",", explode( "\t", trim( $category['num'], "\t" ) ) );
}
}

// content
list($contentRecords, $contentMetaData) = getRecords(array(
'tableName' => 'content',
'where' => " category IN ($selectedBranchNums) ",
));


This turns the list of tab separated values into an array, and then into a comma separated list.

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/

Re: [Jason] Multiple Category Issue

By Perchpole - March 3, 2011

Hi, Jason -

Thanks for the code. I've updated my page but it doesn't quite work as expected.

I assign a record to multiple categories but it only appears on the first category in the list.

Curiously, this is exactly how it worked before I swapped my code for your amended code!

:0/

Perch

Re: [Perchpole] Multiple Category Issue

By Perchpole - March 3, 2011

I think I've cracked it:
'where' => "'%\t{category}\t%' IN ($selectedBranchNums) ",

It seems to work!

???

Perch

Re: [Perchpole] Multiple Category Issue

By Jason - March 7, 2011

Hi,

Great! Glad everything is working for you now.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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