Sub-categories Tutorial

134 posts by 17 authors in: Forums > CMS Builder
Last Post: August 7, 2012   (RSS)

By markr - March 11, 2012

Luv the forum, esp old gems like this post.

Works great, but here's a quick question, can I have multiple category sections each with its own article section?

apple_cats
apple_articles

banana_cats
banana_articles

etc.

I thought it would be simple but when I swap in the table names I either get a function error or a cms_categories not found error.

Re: [markr] Sub-categories Tutorial

By Jason - March 12, 2012

Hi,

Would you be able to attach your code so we can take a look? If you can also let us know which table names you need to use we should be able to show you where the changes need to be made.

Thanks,
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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

By markr - March 12, 2012

Okay, live & learn...

As I started typing this reply I realized I had changed everything to the new table names except the List Options Tablename for 'category'.

Works great with the new table names.

Thx anyway.

Re: [markr] Sub-categories Tutorial

By Jason - March 13, 2012

Hi,

No problem, glad everything is working now.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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

By Mikey - April 29, 2012 - edited: April 29, 2012

Jason or anyone who can help,

Can you shed some light on how to get a three tier category generated menu to work?Or better yet a 4 tier menu is exactly what I need. Here's some code that shows what I've been trying to achieve with a 3 tier menu, but obviously it doesn't work properly and displays both the second and third tier categories of each record under all top tire categories as seen in the attached snap shots.

<!-- category generated menus -->
<div id="nav-container">
<ul id="topnav">

<!-- First tier navigation -->
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php if ($categoryRecord['depth'] == 0): ?>
<li><a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>

<!-- second tier navigation -->
<div class="sub"><ul><li>
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php if ($categoryRecord['depth'] == 1): ?>
<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php endif ?>
<?php endforeach; ?>

<!-- third tier navigation -->
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php if ($categoryRecord['depth'] == 2): ?>
<li style="margin-left:32px">
<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>
</li>
<?php endif ?>
<?php endforeach; ?><!-- /third tier navigation -->

</li></ul><!-- /sub --><!-- /second tier navigation --></div>

</li>
<?php endif ?>
<?php endforeach; ?>
<!-- /First tier navigation -->

<!-- /topnav --></ul>
<!-- /nav-container --></div>
<!-- /category generated menus -->

Re: [zick] Sub-categories Tutorial

By Jason - April 30, 2012

Hi,

What's happening in your code is that for each top level category, you are looping through the entire category list 2 more times. The only way to make this would would be to to check to see if the parent category appeared in the lineage field of each subsequent category. The other problem that you're running into is that you use the $categoryRecord variable in each foreach loop, effectively over writing the value it held in the outer loop.

The easiest way to output the category structure is to use the _listItemStart and _listItemEnd fields. This will output everything into a properly nested <ul> list.

For example:

<ul>

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

<?php echo $category['_listItemStart'];?>

<a href = "<?php echo $category['_link'];?>"><?php echo $category['name'];?></a>

<?php echo $category['_listItemEnd'];?>

<?php endforeach ?>

</ul>


There are a large number of jquery menu scripts available that can use this structure. Here is one I've used in the past:

http://www.dynamicdrive.com/dynamicindex1/ddsmoothmenu.htm#

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/

By Mikey - April 30, 2012 - edited: April 30, 2012

So it sounds like Id need to use something like jQuery to stylize my category levels, using functions such as that below... am I on the right track?
<style>
#navigation {color:gray;width;900px}
.menus {width:130px;float:left;}
li {color:black;}
li.tierone {color:green;}
li.tiertwo {color:blue;}
li.tierthree {color:red;}
li.tierfour {color:purple;}
</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script>
$(function() {
$('li:child').addClass('tierone');
('li:nth-child').addClass('tiertwo');

});
</script>



<div id="navigation">
<ul class="menus">

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

<?php echo $category['_listItemStart'];?>

<a href = "<?php echo $category['_link'];?>"><?php echo $category['name'];?></a>

<?php echo $category['_listItemEnd'];?>

<?php endforeach ?>

</ul>
</div>

Re: [zick] Sub-categories Tutorial

By Jason - May 1, 2012

Hi,

I think I see where you're going with this. It would probably be easier to use a script that will take care of the menu structure for you.

Thanks,
---------------------------------------------------
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: [cjpritchard] Sub-categories Tutorial

By Jason - May 15, 2012

Hi,

It looks like the issue here is that your drop down field is a multi-value list, which means it's tab (\t) separated. This is why you can't use a simple "==" comparision operator.

An easier approach here would be to just retrieve all the faq records for each individual category.

For example:

<?php foreach ($faq_categoriesRecords as $categoryRecord): ?>

<h1><?php echo $categoryRecord['category_name'] ?></h1>
<?php $faqsRecords = mysql_select("faqs", "faq_categories LIKE '%\t".intval($categoryRecord['category_name'])."\t%'"); ?>

<?php foreach($faqsRecords as $record): ?>

<h2><?php echo $record['question'] ?></h2>
<p><?php echo $record['content'] ?></p>

<?php endforeach ?>

<?php endforeach ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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