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

Hi, Jason -

I've done a bit more work and think I have an understanding of what it going on. I've set-up my page templates to <?php echo ?> various details of the set-up on a category page...

In this case the category page is number 3. When I view this page and echo the $selectedBranchNums I get : 0,3

I want to show a record on this page. It is assigned to Categories 3, 4 and 6.

The record shows up on the category 3 page as expected.

I added a line of code to the record so I could see the $record['category']. It displays: 3 4 6 (with a tab between each number).

However, when I go to the Category pages 4 and 6 there is no sign of the record.

As I said in the post above this one, the record only shows up on the first category in the selected list.

It seems to me (with my limited understanding) that there could be something wrong with the 'where' statement below...
// content
list($contentRecords, $contentMetaData) = getRecords(array(
'tableName' => 'content',
'where' => " category IN ($selectedBranchNums) ",
));


Presumably it needs to ask 'where' any one of these numbers (3 4 6) appears IN any of these numbers (0,3).

Currently I'm not entirely sure if it does say this?

Am I on the right tracks?

:0)

Perchpole

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