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
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?
www.deguate.com
Re: [svsanchez] Sub-categories Tutorial
By Jason - June 1, 2010
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
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 -->
www.deguate.com
Re: [svsanchez] Sub-categories Tutorial
By Jason - June 2, 2010
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: [Jason] Sub-categories Tutorial
By svsanchez - June 2, 2010
http://www.casahermes.com/techos.php
www.deguate.com
Re: [svsanchez] Sub-categories Tutorial
By Jason - June 2, 2010
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
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!).
www.deguate.com
Re: [svsanchez] Sub-categories Tutorial
By Jason - June 3, 2010
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
www.deguate.com
Re: [svsanchez] Sub-categories Tutorial
By svsanchez - June 8, 2010
- 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!
www.deguate.com