Blog Questions - Categories

7 posts by 4 authors in: Forums > CMS Builder
Last Post: February 6, 2012   (RSS)

By dwellingproductions - January 24, 2012

Hello!

This post refers to a blog I'm creating here: http://www.ihelpinc.net/Blog/category.php


I'm running into a couple of issues getting categories to work. I'm sure there is a much easier way to do what I'm trying to do and hope someone can enlighten me. :-)


Basically, in CMSB, I have two sections for the blog. One is a multi which is the Blog post section. Here the client can create new blog posts.

The next section is a multi where the client can define categories. These categories are pulled into the Blog post section as a list (as described here: http://www.interactivetools.com/kb/article.php?Populate-a-list-field-from-another-section-15), and the client can select one or more categories which the blog post belongs to. So far so good. :-)

I am also successfully displaying all the categories in the category section on the page as you will see in my example blog page.

1. The problem I have is that I cannot for the life of me figure out how to code the categories so that when clicking on one particular category, all blog posts that belong to that category will be displayed on the page as a list. (An example list can be seen here: http://www.ihelpinc.net/Blog/index.php)

2. It would also be really great to have only categories with associated content displayed in the "Categories" area on the blog page. So, in my example, there are no blog posts marked as belonging to the "insurance" category. So, it would be great to have it so that the word "Insurance" would not show up in the category list on the right side of the page.


Thanks, in advance, for any assistance in getting this working.

Kind regards,
Jeremy
---------------------------

Dwelling Productions

www.dwellingproductions.com

Re: [dwelling] Blog Questions - Categories

By Jason - January 24, 2012

Hi Jeremy,

It looks like you're well on your way to getting this to work, you just need to make a couple of adjustments.

The first thing I would do is change the links that your categories are outputting to put the word "category" in the url. For example:
www.ihelpinc.net/Blog/category.php?category=2

Next, we need to change how we're retrieving blog records. From what you've described, it sounds like your category list field in "blog" is a multi-value select. In CMS Builder, multi-value fields are stored as strings separated by tab (\t) characters. What we can do is look for the category variable in the URL. If we find it, create a custom WHERE clause to only retrieve records where that number appears in the list.

For example:

$where = "";

if (@$_REQUEST['category']) {
$where = "category LIKE '%\t".intval(@$_REQUEST['category'])."\t%'";
}

list($blogRecords, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'allowSearch' => false,
'where' => $where,
));


Note, you may need to change some names to match what is in your sections.

Finally, when outputting your list of categories, you can use the mysql_count function to check if any blog records exist for that category, if not, skip it.

Example:

<?php foreach ($categoryRecords as $category): ?>

<?php if (!mysql_select("blog", "category LIKE '%\t".$category['num']."\t%'")) { continue; } ?>

<a href = "category.php?category=<?php echo $category['num'];?>"><?php echo $category['title'];?></a>

<?php endforeach ?>


Hope this helps get you started.
---------------------------------------------------
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] Blog Questions - Categories

By dwellingproductions - January 24, 2012

Thanks Jason!!!

Oh my goodness... that is absolutely perfect! It now functions EXACTLY as I hoped it would.

The amazing kindness and support from Interactive Tools (along with the ingenious CMS Builder) is why I'll never use any other CMS system. :-)

Thank you so much for your assistance!

All the best!

- Jeremy
---------------------------

Dwelling Productions

www.dwellingproductions.com

Re: [celuch] Blog Questions - Categories

By (Deleted User) - February 3, 2012

Hi celuch,

Could you expand on what you want to retrieve with the multiple categories selected? It'll make giving an example easier!

Thanks,

Tom

Re: [Tom P] Blog Questions - Categories

By celuch - February 6, 2012

I have a CMS table of categories that a product may fall into that contains almost all possible future options. I'd like to show a list of only those categories that actually have entries in them.

I believe it is basically what you did above, only with an external SQL table populating the list.
celuch

Re: [celuch] Blog Questions - Categories

By (Deleted User) - February 6, 2012 - edited: February 17, 2012

Hi celuch,

It sounds like you want to populate a select box with only options for which there are potential results, in which case you need something like

<?php



// Get categories
list($categories,$categoriesMetaData) = getCategories(array(
'tableName' => 'categories_table',
));

// Set the cateogry we're looking for
$where='';
if ( @$_REQUEST['category'] ) { $where = "category='".$_REQUEST['category']."'"; }

// Get records
list($records,$recordsMetaData) = getRecords(array(
'tableName' => 'records_table',
'where' => $where,
));

// Find all the categories with records
$categoriesWithRecords = array();
foreach ( $records as $record ) {
$categoriesWithRecords[] = $record['category'];
}
$categoriesWithRecords = array_unique($categoriesWithRecords);


?>

<select name='category'>
<?php foreach ( $categories as $category ) : ?>
<?php if ( in_array($category['num'],$categoriesWithRecords) ) : ?>
<option value='<?php echo $category['num']; ?>'><?php echo $category['name']; ?></option>
<?php endif; ?>
<?php endforeach; ?>


This will find all the categories with records by looping through all records and adding the category of each record to an array $categoriesWithRecords (which we then reduce to have just unique values).

We then compare every category we've got to make sure that the category does have records before showing that category as an option.

If the same page is used to view the results of a selection, you will need to add 'allowSearch'=>false, to both records retrieval functions to make sure the list of results isn't searched against and add two more calls to those functions to deal with the searching part.

Let me know if this helps,

Tom