Category List with Sub Items

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

By Perchpole - April 28, 2010

I want to set-up a list of categories, each with a sub-list of articles beneath them. Something like this...


Category 1
Article 1
Article 2

Category 2

Category 3
Article 1
Article 2
Article 3


I can do this by using a pair of foreach loops, something like this...


<?php foreach ($categoriesRecords as $category): ?>
<b><?php echo $category['name'] ?></b><BR />
<?php foreach ($articleRecords as $article): ?>
<?php if ($article['category'] != $category['num']) { continue; } // skip records which aren't in this category ?>
<?php echo $article['title'] ?><BR />
<?php endforeach ?>
<?php endforeach ?>


However, as you can see from the example listing above, Category 2 is empty. For my particular application, I want empty categories excluded from the listing. I want it to look like this...

Category 1
Article 1
Article 2

Category 3
Article 1
Article 2
Article 3


How can I re-write the code to achieve this?

:0)

Perchpole

Re: [Perchpole] Category List with Sub Items

By Jason - April 28, 2010

Hi,

One way we can do this is to first get a count of how many articles are in each category. You can do with by putting this code above where you are displaying your categories:

<?php
$articleCount=array();
foreach($articleRecords as $article){
@$articleCount[$article['category']]++;
}
?>


$articleCount now stores a total for each category that has articles in it. Now you just have to change the code where you display your articles like this:

<?php foreach ($categoriesRecords as $category): ?>
<?php if(@$articleCount[category['num']) : ?>
<b><?php echo $category['name'] ?></b><BR />
<?php endif ?>
<?php foreach ($articleRecords as $article): ?>
<?php if ($article['category'] != $category['num']) { continue; } // skip records which aren't in this category ?>
<?php echo $article['title'] ?><BR />
<?php endforeach ?>
<?php endforeach ?>


Give that a try. Let me know if you run into any issues.
---------------------------------------------------
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 - May 19, 2010

Hey Jason,

I'm a bit confused. I'm trying to do something similar. Need I set up a Category Menu for this to work?

What I did: I setup a Multi Record Menu and added a List field.

Re: [hordak] Category List with Sub Items

By Jason - May 19, 2010

Hi,

You don't need to have a category menu to make this work. If you can attach the code you have so far and let me know exactly what you're trying to do, I can take a look.

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] Category List with Sub Items

By hordak - May 19, 2010

Jason, I think I massacred the script....

I've got the following fields:
Title - title
Department - department

I'd like the following layout (like above inquiry)
Department 1
- Title 01

Department 2
- Title 02
- Title 03

My code looks as such (please don't laugh...)
--- Head code ----
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/cmsAdmin/lib/viewer_functions.php";

list($as_humanities_faclistRecords, $as_humanities_faclistMetaData) = getRecords(array(
'tableName' => 'as_humanities_faclist',
));

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


---------Body code -------------
<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
<?php if ($as_humanities_faclistRecords): ?>
<div id="popup" style="display: none;"></div>
<?php foreach ($departmentRecords as $department): ?>
<?php if(@$titleCount[title['num']) : ?>
<b><?php echo $department['department'] ?></b><BR />
<?php endif ?>
<?php foreach ($titleRecords as $title): ?>
<?php if ($title['department'] != $title['num']) { continue; } // skip records which aren't in this category ?>
<?php echo $title['title'] ?><BR />
<?php endforeach ?>
<?php endforeach ?>
<?php endif ?>

<?php if (!$as_humanities_faclistRecords): ?>
<li>No records were found!</li>
<?php endif ?>

<!-- STEP2a: /Display Uploads -->

Re: [hordak] Category List with Sub Items

By Jason - May 19, 2010

Hi,

It looks like you're pretty close actually. I think we only need to make one small change.
The variable $titleCount would be a count of how many titleRecords appear in each department. So we need to access the count for any given department number. Currently, we're looking at the title number.

So we need to change this line:

<?php if(@$titleCount[title['num']) : ?>

to this:

<?php if(@$titleCount[$department['num']) : ?>

Now, this does assume that the Title table is storing the department number and not the department title in each record.

Give this a try and let me know how it works for you.
---------------------------------------------------
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 - May 20, 2010 - edited: May 20, 2010

Oh wait! title is not my table name, title is just the field. the table name is as_humanities_faclist

Within Table as_humanities_faclist
are fields:
- department (list)
- title (text field)

Per your last message, i've updated the as follows...
<?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): ?>
<?php if (@$titleCount[$department['Africana Studies']]): ?>
<span class="text"><strong><?php echo $department['department'] ?></strong></span>
<?php endif ?>

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


now i'm getting the attached results (see attachment )... sigh..LOL
Attachments:

picture-1_005.png 89K

Re: [Jason] Category List with Sub Items

By hordak - May 20, 2010

Hey Jason,

Not bad! the department & title are displaying.

Only problem: The array displays multiple duplicate Department/Title entries. It displays 17 duplicate entries. I have seven total faculty listings.

I'm trying to think out the logic; is it because....

The reason for this is that for each iteration of the loop, PHP takes 1 record from $as_humanities_faclistRecords and assigns it to $title.

Shouldn't take 1 record from $as_humanities_faclistRecords and apply it to a department? ($department)

Re: [hordak] Category List with Sub Items

By hordak - May 20, 2010

It's funny... I have 17 department & faculty sets. I have 17 faculty entries ('title' field).