Bucking the Query Filter!
8 posts by 3 authors in: Forums > CMS Builder
Last Post: April 29, 2010 (RSS)
By Perchpole - April 19, 2010
By using categories we can now filter the results of a listpage. So, for example, if my url is ".../xyz.php?category=2" the list page will show only those items assigned to category 2. CMSB filters the results automatically which is a very useful feature.
However, is it possible to selectively bypass this mechanism?
Here's an example of where it would be useful. The following describes a simple category tree (category numbers in brackets)...
(1) Home
(2) About
(3) Articles
(4) - Food
(5) - Drink
(6) Contact
Category 3 (Articles) is a container for the two sub-categories, Food and Drink. It does not have any articles of its own.
So, when I go to ".../xyz.php?category=3" the list will be empty.
Ideally, what I want to do is show the articles assigned to the two sub-categories instead.
Currently CMSB does not want to allow me to do this! Whilst I remain on ".../xyz.php?category=3" it will only list category 3 items.
How can I force it to display articles from sub-categories?
:0)
Perch
Re: [Perchpole] Bucking the Query Filter!
By Dave - April 19, 2010
There's a few options, but let's start with a few questions:
How are you specifying subcategories? Is this a multi-record section or a "category" section?
And are there multiple instances of subcategories in your list or just the provided example (if searching category 3 also include 4 and 5)?
interactivetools.com
Re: [Dave] Bucking the Query Filter!
By Perchpole - April 19, 2010
Thanks for your prompt response.
In answer to your questions..
1) The page is a multi-record section which also incorporates a category menu. So there are instances of...
list($categoryRecords, $selectedCategory)...
...and...
list(itemRecords, itemMetaData)...
...at the top of the page
2) Let's assume there will be multiple sub-categories present. That seems to offer the most flexible approach.
:0)
Perch
Re: [Perchpole] Bucking the Query Filter!
By Perchpole - April 20, 2010
Any joy with this one?
:0)
Perch
Re: [Perchpole] Bucking the Query Filter!
By Dave - April 23, 2010
Sorry for the delay. Anytime you start working multiple multiple sections it can get a bit tricky and start to require some custom code. I'll try to point you in the right direction.
There's two steps:
First, getting a list of category numbers that you want to select itemRecords from. Here's some sample code for that:
// get category nums of selected branch, eg: 0,3,4,6,7
$selectedCategoryNum = $_REQUEST['category'];
$selectedBranchNums = '0';
foreach ($categoryRecords as $category) {
if ($category['_isSelected'] || $category['_isAncestorSelected']) {
$selectedBranchNums .= ',' . (int) $category['num'];
}
}
Next, passing that to your item list viewer so it only returns items from those categories:
...
'allowSearch' => false,
'where' => " category IN ($selectedBranchNums) ",
And basically you should end up with a WHERE like this: category IN (0,3,4,5,6) that only shows items in those categories.
Hope that helps!
interactivetools.com
Re: [Dave] Bucking the Query Filter!
By Perchpole - April 25, 2010
Fantastic. Yet again!
What will I do when I run out of superlatives!
Thanks,
:0)
Perch
Re: [Perchpole] Bucking the Query Filter!
By Perchpole - April 28, 2010
Just a quick supplementary question on this issue.
Although your method is perfect for what I want, using means I loose the ability to call pages by their URL...
whereRecordNumberInUrl(1)
I've tried various ways of splicing the two strings together...
category IN ($selectedBranchNums) && whereRecordNumberInUrl(1)
...but nothing seems to work.
Can I have the best of both worlds?
:0/
Perch
Re: [Perchpole] Bucking the Query Filter!
By Jason - April 29, 2010
You could try this:
'allowSearch' => false,
'where' => " category IN ($selectedBranchNums) AND num=".whereRecordNumberInUrl(1),
Give that a try.
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/