Blog Questions - Categories

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

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

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: [dwelling] Blog Questions - Categories

By celuch - February 2, 2012

Hi Jason

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

Thanks!
celuch

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