Sub-categories Tutorial

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

By svsanchez - May 31, 2010 - edited: May 31, 2010

Hello, thank you for this excellent post, I would like to know how to do the following:

My customer needs his site to show the MAIN category on a column, then subcategories from the selected MAIN category on a second column, and lastly products from she selected subcategory on the third column, like this:

MAIN CATEGORY 1 | Subcat 1 from selected main Category | List of products
MAIN CATEGORY 2 | Subcat 2 from selected main Category | from selected subcat
MAIN CATEGORY 3 | Subcat 3 from selected main Category |
| Subcategory 4 from selected main Category |
| Subcategory 5 from selected main Category |

I know how to show only the MAIN CATEGORY with the 'categoryFormat' => 'onelevel', tag, but how do I only show the Subcategories from a selected Category?
Sven Sanchez

www.deguate.com

Re: [svsanchez] Sub-categories Tutorial

By Jason - June 1, 2010

Hi,

If you could attach the .php file you're currently working with, I can take a closer look at this 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: [Jason] Sub-categories Tutorial

By svsanchez - June 1, 2010

Hello Jason, thank you for your reply. For the moment I have only done what is explained on your tutorial (which I have pasted below) but what I need to achieve is in the following URL (in yellow my comments): www.casahermes.com/new/cms.htm

For the code I have as of now, as I said it's only the one from your example with the difference that I used "twolevel" instead of "onelevel" to see if it would show me only the subcategories but of course this didn't work :(

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';




$dirsToCheck = array('/home/casahem/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records

list($categoriesRecords, $selectedCategory) = getCategories(array( 'tableName' => 'categories', 'categoryFormat' => 'twolevel', ));
list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',

));

?>


<h1>Categories</h1>
<ul>
<li><a href="?">(All Articles)</a></li>

<?php foreach ($categoriesRecords 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 endforeach ?> </ul>



<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
<h1>Articles - List Page Viewer</h1>
<?php foreach ($articlesRecords as $record): ?>

<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br/>

<?php endforeach ?>

<?php if (!$articlesRecords): ?>
No records were found!<br/><br/>
<?php endif ?><!-- /STEP2: Display Records -->
Sven Sanchez

www.deguate.com

Re: [svsanchez] Sub-categories Tutorial

By Jason - June 2, 2010

Hi,

What we need to do is output our categories in two different loops: one for the top level, one for the second level.

For the first level, you can use a loop like this:

<?php $selectedCat=""; ?>
<?php foreach($categoryRecords as $category):?>
<?php if(!$category['_hasParent']):?>
<?php echo $category['_listItemStart']; ?>
<?php if($category['_isSelected']){echo "<b>"; $selectedCat=$category['num'];} ?>
<a href="?category=<?php echo $category['num']?>"><?php echo $category['name']; ?></a>
<?php if($category['_isSelected']){echo "</b>";} ?>
<?php echo $category['_listItemEnd']; ?>
<?php endif ?>
<?php endforeach ?>


This will only output categories that do not have a parent (top level).

The second column would be outputted like this:

<?php if($selectedCat): ?>
<?php foreach($categoryRecords as $category): ?>
<?php if ($selectedCat==$category['parentNum']):?>
<?php echo $category['_listItemStart']; ?>
<?php echo $category['name']; ?>
<?php echo $category['_listItemEnd']; ?>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>


This will only output if a top level category has been selected. And then will only output categories whose parent was selected.

You may need to change some names to match what you have in the database. You will also need to add an <a> tag around the second column items.

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: [svsanchez] Sub-categories Tutorial

By Jason - June 2, 2010

Hi,

The easiest change would be to change where you`re selecting your category records.

Try changing this:
list($categoriesRecords, $selectedCategory) = getCategories(array( 'tableName' => 'categories', 'categoryFormat' => 'twolevel', ));
to this:
list($categoryRecords, $selectedCategory) = getCategories(array( 'tableName' => 'categories', 'categoryFormat' => 'twolevel', ));

That should take care of the error for you.
Let me know if you run into any other problems.
---------------------------------------------------
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 svsanchez - June 3, 2010

Hello Chris, you're the man! This is excellent!

Can you tell me how to remove the bulleted list (I would like to use a little image to the left of each menu item instead of bullets!).
Sven Sanchez

www.deguate.com

Re: [svsanchez] Sub-categories Tutorial

By Jason - June 3, 2010

Hi,

Glad to hear that's working out for you.

There are two ways to get an image there.

1) you can use CSS to set the background image of the list tag
example:
li
{
padding-left: 10px;
background-image: url(images/arrow.gif);
background-repeat: no-repeat;
background-position: 0 .5em;
}


Or, you can remove the code that looks like this:
<?php echo $category['_listItemEnd']; ?>

and replace:

<?php echo $category['_listItemStart']; ?>
with some kind of <img> tag.

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 svsanchez - June 3, 2010

Sorry Jason, I don't know why I wrote "chris" on my last post... what you just suggested worked as a marvel, many thanks for your excellent support!!!! It is very de-stressing to know we can count on you guys!
Sven Sanchez

www.deguate.com

Re: [svsanchez] Sub-categories Tutorial

By svsanchez - June 8, 2010

Hello, I am encountering a new problem: when I select a sub menu, it will show me the products inside but the sub menu disappears. If you can go to www.casahermes.com/techos.php you will see your code in action in the first and second columns. If you click on the first column menu on "techos" you get the second column menu with 3 options:

- Fibrocemento
- Metálicos
- Plasticos

The Fibrocemento option has a few test products inside, please click on it and you will see them displayed on the 3rd column, but the sub menu disappears!
Sven Sanchez

www.deguate.com