Category List with Sub Items

15 posts by 3 authors in: Forums > CMS Builder
Last Post: June 4, 2010   (RSS)

By Jason - May 20, 2010

Hi,

In PHP, a foreach loop works like this:
foreach(*Set of Records* as *Single Record from the Set* )

But that's not what's happening here. What's happening is that our list of categories (departments) and the records under those categories (titles) are all stored in the same table. That's why we're getting exactly double what we should.

We can get around this by creating a list of unique department name. Try this:

<?php
$titleCount=array();
$departments=array();

foreach($as_humanities_faclistRecords as $title){
@$titleCount[$title['department']]++;
}

foreach($as_humanities_faclistRecords as $department){
if(!@$departments[$department[department]]){$departments[$department[department]]=$department['department'];}
}
?>

<?php if ($as_humanities_faclistRecords): ?>
<div id="popup" style="display: none;"></div>

<?php foreach ($departments as $department): ?>
<?php if (@$titleCount[$department]): ?>
<span class="text"><strong><?php echo $department ?></strong></span>
<?php endif ?>

<ul class="list" style="margin:2px;">
<?php foreach ($as_humanities_faclistRecords as $title): ?>
<?php if ($department != $title['department']) { continue; } // skip records which aren't in this category ?>
<li><?php echo $title['title'] ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
<?php endif ?>


So the variable $departments will be an array of department names (each one only appearing once).

Give this a try and let me know how you do.
---------------------------------------------------
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] Category List with Sub Items

By hordak - June 3, 2010

Thanks Jason! I was pulled from this project for awhile, but I finally got back to it. The code you assisted with is GREAT! I customized it with an extra field for additional information. I have one last question:

1. How can i make this code display Departments that don't contain "title" data?

This was the previous question, but I can't find what's controlling this?

Once again thanks!
-------------------------My Code -------------------

<?php
$titleCount=array();
$departments=array();

foreach($as_humanities_faclistRecords as $title){
@$titleCount[$title['department']]++;
}

foreach($as_humanities_faclistRecords as $department){
if(!@$departments[$department['department']]){$departments[$department['department']]=$department['department'];}
}
?>

<?php if ($as_humanities_faclistRecords): ?>
<div id="popup" style="display: none;"></div>

<?php foreach ($departments as $department): ?>
<?php if (@$titleCount[$department]): ?>
<span class="text"><strong><?php echo $department ?></strong></span>
<?php endif ?>

<ul class="list" style="margin:2px;">
<?php foreach ($as_humanities_faclistRecords as $title): ?>
<?php if ($department != $title['department']) { continue; } // skip records which aren't in this category ?>
<li><?php echo $title['title'] ?>
<?php if ($title['content']): ?>
| <a class="zoom1" href="<?php echo $title['_link'] ?>">Info</a>
<?php endif ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
<?php endif ?>

By Jason - June 3, 2010

Hi,

So you want to display all Departments (even ones without any title data) or do you want to display a list of Departments that don't have any title data separately?

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/

By Jason - June 4, 2010

Hi,

If we want to display department names regardless as to whether or not they will have any entries below them, we can change our code to this:

<?php
$titleCount=array();
foreach($as_humanities_faclistRecords as $title){
@$titleCount[$as_humanities_faclist['department']]++;
}
?>

<?php if ($as_humanities_faclistRecords): ?>
<div id="popup" style="display: none;"></div>

<?php foreach ($as_humanities_faclistRecords as $department): ?>
<span class="text"><strong><?php echo $department['department'] ?></strong></span>

<ul class="list" style="margin:2px;">
<?php foreach ($as_humanities_faclistRecords as $title): ?>
<?php if ($as_humanities_faclist['department'] != $title['Africana Studies']) { continue; } // skip records which aren't in this category ?>
<li><?php echo $title['title'] ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
<?php endif ?>


This should do what 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/