Simple Sub-Category Markup

11 posts by 2 authors in: Forums > CMS Builder
Last Post: July 5, 2010   (RSS)

By theclicklab - June 28, 2010

Hi there,

I have a Category menu setup and just want to generate a 2 level list from this without any selected options.

Is there an existing tutorial out there for this?

All I really need to generate is the following:

<ul>
<li>Top Level
<ul>
<li>Second Level</li>
<li>Second Level</li>
<li>Second Level</li>
</ul>
</li>
<li>Top Level</li>
<li>Top Level</li>
</ul>


Many thanks!

Re: [aquaman] Simple Sub-Category Markup

By Jason - June 29, 2010

Hi,

Take a look at this and see if it gets you started:
http://www.interactivetools.com/forum/gforum.cgi?post=77230#77230

If you run into any problems, just let me know.
---------------------------------------------------
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] Simple Sub-Category Markup

By theclicklab - June 29, 2010

Hi Jason, Thanks for that, but I have had serious trouble trying to get anything to work after battling with it for hours.

This is what I currently have which works for a single level but I need to get the second level showing where marked.

Note: There is no selected state needed as this is a site wide menu.

<?php
list($destinationsRecords, $destinationsMetaData) = getRecords(array(
'tableName' => 'destinations',
'where' => "active='1' AND hide='0' AND depth='0'",
'allowSearch' => '0',
'useSeoUrls' => true,
));
?>

<li><a href="/destinations.php">Charter Destinations</a>
<ul>
<?php foreach ($destinationsRecords as $record): ?>
<li><a href="/<?php echo $record['_link'] ?>"><?php echo $record['name'] ?></a>

***
<ul>
<li>INSERT SECOND LEVEL ITEMS HERE</li>
<li>INSERT SECOND LEVEL ITEMS HERE</li>
</ul>
***

</li>
<?php endforeach ?>
</ul>
</li>


Thanks again for you help!

Re: [aquaman] Simple Sub-Category Markup

By Jason - June 30, 2010

Hi,

If your records in the "destinations" table are set up as categories, you can try this to display your categories:

<?php
list($destinationsRecords, $destinationsMetaData) = getCategories(array(
'tableName' => 'destinations',
'categoryFormat' => 'showAll',
'allowSearch' => '0',
'useSeoUrls' => true,
));
?>

<li><a href="/destinations.php">Charter Destinations</a>

<?php foreach ($destinationsRecords as $record): ?>
<?php echo $record['_listItemStart']; ?>
<a href="<?php echo $record['_link'];?>"><?php echo $record['name']'?></a>
<?php echo $record['_listItemEnd'];?>
<?php endforeach ?>
</li>


Give this a try and see if it's giving you the results you're looking for.

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] Simple Sub-Category Markup

By theclicklab - July 1, 2010

Hi Jason, This worked a treat but I have run into a snag...

I have some records in here which are either active or hidden. So I tried to add this:

'where' => 'active ="1"' . ' AND hide ="0"',

which generates the following error:

Category Viewer (destinations) errors
Unknown option 'where' specified
Valid option names are: (tableName, useSeoUrls, debugSql, selectedCategoryNum, categoryFormat, loadUploads, defaultCategory, rootCategoryNum, ulAttributes)

So how do I filter out these if I cant use "where"?

Many thanks
Jan

Re: [aquaman] Simple Sub-Category Markup

By Jason - July 2, 2010

Hi Jan,

We can check for these values as part of our foreach loop like this:

<li><a href="/destinations.php">Charter Destinations</a>

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

<?php if($record['active']==1 && $record['hide']==0): ?>

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

<?php endif ?>
<?php endforeach ?>
</li>


This if statement will only allow allow categories where active=1 and hide=0 to appear.

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: [aquaman] Simple Sub-Category Markup

By Jason - July 5, 2010

Hi,

That's an interesting problem. I think we're going to have to set a variable to check to see, when we're outputting a parent record, if the previous record was an inactive child record.

We can try something like this:

<li><a href="/destinations.php">Charter Destinations</a>

<?php $inactiveChild=0; ?>
<?php foreach ($destinationsRecords as $record): ?>

<?php if($record['active']==1 && $record['hide']==0): ?>
<?php if($inactiveChild && !$record['hasParent']): ?>
</ul>
<?php endif ?>
<?php $inactiveChild=0; ?>
<?php echo $record['_listItemStart']; ?>
<a href="<?php echo $record['_link'];?>"><?php echo $record['name'];?></a>
<?php echo $record['_listItemEnd'];?>
<?php else: ?>
<?php if($record['_hasParent']): ?>
<?php $inactiveChild=1;?>
<?php else: ?>
<?php $inactiveChild =0; ?>
<?php endif ?>
<?php endif ?>
<?php endforeach ?>
</li>


Give this a try.

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] Simple Sub-Category Markup

By theclicklab - July 5, 2010

Thanks Jason, I got that working, just have one last snag to deal with.

If the inactive item is the first in the second level, I'm missing an opening <ul>.

Tried tweaking to solve the issue but failed to get it.

Here's the output:

<li><a href="/destinations.php">Charter Destinations</a>
<ul class="top">
<li> <a href="top-level-item">top level item</a> </li>
<li> <a href="top-level-item">top level item</a>
<ul >
<li> <a href="second-level">second level</a> </li>
<li> <a href="second-level">second level</a> </li>

</ul> *** This works - UL added here when last item inactive or hidden ***

<li> <a href="top-level-item">top level item</a> </li>

*** Missing UL here - when first item inactive or hidden ***

<li> <a href="second-level">second level</a> </li>
<li> <a href="second-level">second level</a> </li>
</ul>
</li>
<li> <a href="top-level-item">top level item</a> </li>
<li> <a href="top-level-item">top level item</a> </li>
</ul>
</li>

Re: [aquaman] Simple Sub-Category Markup

By Jason - July 5, 2010

Hi,

I think we pretty much have it now.

Try this code (I just modified the last code I posted)

<li><a href="/destinations.php">Charter Destinations</a>

<?php $inactiveChild=0; ?>
<?php $childCount=0; ?>
<?php foreach ($destinationsRecords as $record): ?>

<?php if($record['active']==1 && $record['hide']==0): ?>
<?php if($inactiveChild && !$record['hasParent']): ?>
</ul>
<?php endif ?>
<?php if($record['hasParent']): ?>
<?php $childCount++; ?>
<?php else:?>
<?php $childCount=0;?>
<?php endif ?>
<?php $inactiveChild=0; ?>
<?php echo $record['_listItemStart']; ?>
<a href="<?php echo $record['_link'];?>"><?php echo $record['name'];?></a>
<?php echo $record['_listItemEnd'];?>
<?php else: ?>
<?php if($record['hasParent'] && $childCount==0): ?>
<ul>
<?php $childCount++; ?>
<?php endif ?>
<?php if($record['_hasParent']): ?>
<?php $inactiveChild=1;?>
<?php else: ?>
<?php $inactiveChild =0; ?>
<?php endif ?>
<?php endif ?>
<?php endforeach ?>
</li>


This code will count the number of children for each parent category. If there's an inactive child and it's the first child of that parent, it outputs a <ul>

Give that a try and let me know how you do.

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/