Sub-categories Tutorial

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

By Jason - March 14, 2011

Hi Nigel,

1. You can use an if statement to only output an article record if it belongs to a selected category (ie, if no category has been selected, no articles appear). For example:

<?php if ($record['category'] == @$selectedCategory['num']): ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br />
<?php endif ?>


2. This is a little trickier since when you're outputting a category, you don't have a list of the articles associated with it. To help you get started, you can tell if a given record has children (ie, sub categories) like this:

<?php if($categoryRecord['_hasChild']): ?>
<li><a href="?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a></li>
<?php else: ?>
//category has not sub categories..output direct link here
<?php endif ?>


3. You can use a query to only get top level categories that won't be affected by selecting categories from the other list. Try something like this:

<?php

$query = "SELECT * FROM `{$TABLE_PREFIX}categories` WHERE depth = 0";
$topLevelCategories = mysql_query_fetch_all_assoc($query);

?>

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

<?php if ($categoryRecord['_isSelected']): ?><?php endif ?>
<p class="subnavTextSmall"><a href="?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a> </p>
<?php if ($categoryRecord['_isSelected']): ?><?php endif ?>

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

Re: [Jason] Sub-categories Tutorial

Many thanks for your help, Jason. I'll give this a go.
Nigel Gordijk

Common Sense Design: User-focused Web design
Tel: 001 519 342 5348 | Web: www.commonsensedesign.net

Re: [Chris] Sub-categories Tutorial

By csdesign - March 18, 2011

Chris - again - great tutorial! Each time I do this they client wants something a little different so I need a little more help.

I have the articles and categories setup and the page is working... kind of. but not in the way I need it too so before I start hacking it to pieces, I need some direction :)

I already had the articles & categories setup so I kept those names, so:

articles = physicians_wyoming
categories = specialites

I've attached my physiciansList.php page and a screenshot of what I'm trying to accomplish and then one marked "under construction" to show you what it looks like now. There will be two separate Physician pages, one listed by specialty and one as a photo/bio list but I think I know how to do that one. I just need help with the specialty list.

If at all possible, I would also like each specialty to go to a page with more information about that specialty, ie: orthopedic page.

Thank you!! Tina

Re: [csdesign] Sub-categories Tutorial

By Jason - March 21, 2011

Hi Tina,

If I understand correctly, you just want to organize all the physicians according to their specialty and then output them underneath each specialty heading. Is that right?

If so what you can do is create an array that arranges all the physician records according to their selected specialty. In this code example, I've had to make a couple of assumptions. First, that you have a field in the physicians_wyoming table called "specialty". I've also assumed that the value of this field is the same as the "num" field coming from the specialites table.

Assuming these things, here's what we can do. First, we loop through all the physicians, and add them to an array based on their specialty number like this:

// organize physician records into specialties
$physicanSpecialityToRecord = array();

foreach( $physicians_wyomingRecords as $physician) {
$physicanSpecialityToRecord[$physican['specialty']][] = $physician;

}


Next, we add code to output these records as we're outputting our specialty records like this: (changes in red)

<?php foreach ($specialtiesRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>

<?php if ($categoryRecord['_isSelected']): ?><b><?php endif ?>
<a href="?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif ?>

<?php echo $categoryRecord['_listItemEnd'] ?>

<?php foreach ($physicanSpecialityToRecord[$categeoryRecord['num']] as $physician): ?>

<a href="<?php echo $physician['_link'] ?>"><?php echo $physician['physician_first_name'] ?>&nbsp;<?php echo $physician['title'] ?></a><br/>

<?php endforeach ?>


<?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] Sub-categories Tutorial

By csdesign - March 21, 2011

Hi Jason,

I'm close... but not close enough - darn. ;) Yes, I would like the specialty name to display with the appropriate dr. names below each specialty - with links to their bio pages.

The field in physicians_wyoming is called "specialties" because some have more than one. To the best of my knowledge, the "num" field is the same because I set it up to get options from db / specialties / num / name (see screenshot) pic4

I changed the word "specialty" to "specialties" throughout the new code also change the typo in category.. but it's still not playing nice. I've attached my physiciansList.php page and a few screenshots.

Is it also possible to get the list to display in 2 columns? I know... I'm just getting greedy now. ;)

here's the urL:
http://www.nwsc5872139.myutilitydomain.com/physiciansList.php

THANKS!!! Tina

Re: [csdesign] Sub-categories Tutorial

By Jason - March 22, 2011

Hi Tina,

The code for actually outputting our list should still work, all we need to do is be able to add a physician to more than one category. In CMS Builder, multi-value lists are stored as strings separated by tab (\t) characters. We can break these strings apart to handle each value individually like this:

// organize physician records into specialties
$physicanSpecialityToRecord = array();

foreach( $physicians_wyomingRecords as $physician) {

// break selected specailties into an array
$specialties = explode("\t", trim($physican['specialties'], "\t"));

foreach ($specialties as $specialty){
$physicanSpecialityToRecord[$specialty][] = $physician;
}

}


As for breaking your list up into 2 columns, there are many different ways of accomplishing that depending on how you're designing your page. Here's an example of using two columns when displaying your content in a table:
http://www.interactivetools.com/forum/gforum.cgi?post=62848#62848

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/

Re: [Jason] Sub-categories Tutorial

By csdesign - March 23, 2011

I'll give that a try. THANKS!!!!

Re: [csdesign] Sub-categories Tutorial

By csdesign - March 23, 2011

worked perfect! Thanks!!

Re: [csdesign] Sub-categories Tutorial

By rez - June 9, 2011 - edited: June 9, 2011

Using Tinas code above, how can I list out all categories with their items under each without anything having to have been selected.

I have a category menu set up and in aanother regular list editor, have items assigned to categories just like above.

But I dont want to select and load anything or show a list of links for navigation. I simply want to list it all out:

category1
item1
item2
category2
category2A
item1
item2
category2B
item1
item2
category3
item1
item2

Probably even easier than above but i dont know how to take out the "if selected code" and simply just list it all out on one page.

thanks.