Question concerning Chris' subcategory tutorial

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

First off all, thanks for creating the tutorial, it has been a great help! Now my question. How do I display the articles of only one category? If I take the tutorial as an example, how do I only display the Dog Articles. So no categories or other articles, just the articles within the category "Dog".

Thanks alot!

Hansie

Re: [Hansaardappel] Question concerning Chris' subcategory tutorial

bump...

Re: [Hansaardappel] Question concerning Chris' subcategory tutorial

By Chris - March 15, 2010

Hi Hansaardappel,

You can do this by generating a List Viewer for your Articles section. You have two options as to how to display only articles in the Dogs category:

(Let's assume that your Dogs category has a num of "1".)

1. Link to your List Viewer with ?category=1

This will cause the getRecords() call to filter articles which belong to other categories.

2. Hard-code the List Viewer to filter articles with a "where" clause. Do to this, change your getRecords() call to:

list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
'where' => 'category = 1',
));


Do these solutions solve your problem?

Please let me know if you have any questions.
All the best,
Chris

Re: [chris] Question concerning Chris' subcategory tutorial

That worked great Chris, thanks. I've got a new problem though [mad]

I'm currently working on adding articles to multiple categories, I changed the list to multi value and chose the appropriate categories for my articles. Displaying articles that only belong to a single category still works fine, but I'm having some problems with articles that belong to multiple categories. There's this one article, that belongs to category "Tournament Results" (Parent category (num=1)), "Indian Wells" (sub category of Tournament Results (num=2)) and to "Roger Federer" (Parent category (num=3)). What I want to do is to only display the articles within category "Roger Federer".

My code:

list($categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'categories',
'categoryFormat' => 'onelevel',
));

list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
'perPage' => '5',
'where' => 'category = 3',
));

// Display code

<?php foreach ($articlesRecords as $record): ?>
<?php echo $record['content'] ?><br/>
<?php endforeach; ?>

<?php if (!$articlesRecords): ?>
No records were found!<br/><br/>
<?php endif ?>

Other articles in the category are being displayed fine, but not this specific article. When I remove the article from the other categories, so that the category "Roger Federer" is the only one it belongs to, it DOES get displayed.

What am I doing wrong?

Re: [Hansaardappel] Question concerning Chris' subcategory tutorial

By Chris - March 18, 2010

Hi Hansaardappel,

CMS Builder stores multi-value list fields as a tab-separated list. It also puts a tab on the front and back of the list for easy searching.

For example, your article's category field will be:

tab 1 tab 2 tab 3 tab

...which is represented as:

"\t1\t2\t3\t"

Finding all the articles which belong to category 3 simply involves looking for category fields which contain "\t3\t". To do that, change your 'where' line to:

'where' => "category LIKE '%\t3\t%'",

I hope this helps. Please let me know if you have any questions.
All the best,
Chris