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

By Jason - May 20, 2010

Hi,


There are a couple of issues here, but they should be pretty easy fixes.

First, you need to change your code where you're creating $titleCount to this:

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


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.

Now, after that I'm seeing this line in your code:

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

This will cause an error because 'Africana Studies' is not a field name in the table.
We need to change this to the kind of values we were using when we created $titleCount, like this:

<?php if (@$titleCount[$department['department']]): ?>
Remember, in this case $department is just a record from $as_humanities_faclistRecords, not a table.

Finally, we need to change the variables that compare so that we skip records that are not in the proper category. So, our final code should look like this:

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

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

<?php foreach ($as_humanities_faclistRecords as $department): ?>
<?php if (@$titleCount[$department['department']]): ?>
<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 ($department['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 ?>


Give this a try and let me know how it goes.

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 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).