Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
Blog Questions - Categories

 

 


dwelling
User

Jan 24, 2012, 5:12 AM

Post #1 of 7 (1092 views)
Shortcut
Blog Questions - Categories Can't Post

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


Jason
Staff / Moderator


Jan 24, 2012, 10:16 AM

Post #2 of 7 (1086 views)
Shortcut
Re: [dwelling] Blog Questions - Categories [In reply to] Can't Post

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:


Code
$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:


Code
<?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 - Programmer 
interactivetools.com

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


dwelling
User

Jan 24, 2012, 6:32 PM

Post #3 of 7 (1078 views)
Shortcut
Re: [Jason] Blog Questions - Categories [In reply to] Can't Post

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


celuch
User

Feb 2, 2012, 4:29 AM

Post #4 of 7 (1030 views)
Shortcut
Re: [dwelling] Blog Questions - Categories [In reply to] Can't Post

Hi Jason

Can you expand this to use a list getting "categories" from a database?

Thanks!


Tom P
User


Feb 3, 2012, 11:34 AM

Post #5 of 7 (1000 views)
Shortcut
Re: [celuch] Blog Questions - Categories [In reply to] Can't Post

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


celuch
User

Feb 6, 2012, 4:42 AM

Post #6 of 7 (955 views)
Shortcut
Re: [Tom P] Blog Questions - Categories [In reply to] Can't Post

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.


Tom P
User


Feb 6, 2012, 12:16 PM

Post #7 of 7 (934 views)
Shortcut
Re: [celuch] Blog Questions - Categories [In reply to] Can't Post

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


Code
<?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


(This post was edited by Jason on Feb 17, 2012, 10:31 AM)