Display Sub-categories in a Simple List

6 posts by 2 authors in: Forums > CMS Builder
Last Post: November 30, 2010   (RSS)

By cjpritchard - November 30, 2010

Hello ... I'm trying to output a simple list of categories but am having a bit of trouble. I've followed Chris's sub-category tutorial (http://www.interactivetools.com/forum/gforum.cgi?post=77230) successfully for other outputs, but can't get this to work.

I have a list of businesses and individuals that fall into one of 9 categories. My desired output is as follows:

Category 1
- business 1
- business 2
- business 3

Category 2
- business 4
- business 5
- business 6

Category 3
- business 7
- business 8
- business 9
- business 10

etc.

But when I output my list each member appears in every category, as in:

Category 1
- business 1
- business 2
- business 3

Category 2
- business 1
- business 2
- business 3

etc.

I know this is a simple coding issue. Here is what I have in my file:

HEAD

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/tucg/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."); }

list($categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'categories',
'categoryFormat' => 'onelevel',
));

list($participantsRecords, $participantsMetaData) = getRecords(array(
'tableName' => 'participants',
));


BODY

<?php foreach ($categoriesRecords as $categoryRecord): ?>
<h2><?php echo $categoryRecord['name'] ?></h2>
<ul>
<?php if ($categoryRecord['num']): ?>

<?php foreach ($participantsRecords as $record): ?>
<li><?php if ($record['url']): ?><a href='http://<?php endif ?>
<?php echo $record['url'] ?><?php if ($record['url']): ?>' target='_blank'><?php endif ?><?php echo $record['title'] ?>
<?php if ($record['url']): ?></a><?php endif ?>
<span class="contact-info"><?php echo $record['phone_number'] ?></li>
<?php endforeach ?>

<?php endif ?>
</ul>

<?php endforeach ?>
</ul>

Any help would be appreciated.
Many thanks,

Chris P

Re: [cjpritchard] Display Sub-categories in a Simple List

By Jason - November 30, 2010

Hi Chris,

What field do you have in participants that associates them with a category? This is the field you need to check when outputting your records,

I'll give you an example of how it should work. In this example, I'll assume you have a field in participants called 'category' that's a drop down menu. I'll also assume that in its value field, you're storing the num field from the category section.

If this is true, here is how we can output these records:

<?php foreach ($categoriesRecords as $categoryRecord): ?>
<h2><?php echo $categoryRecord['name'] ?></h2>
<ul>

<?php foreach ($participantsRecords as $record): ?>
<?php if($record['category']==$categoryRecord['num']):?>

<li><?php if ($record['url']): ?><a href='http://<?php endif ?>
<?php echo $record['url'] ?><?php if ($record['url']): ?>' target='_blank'><?php endif ?><?php echo $record['title'] ?>
<?php if ($record['url']): ?></a><?php endif ?>
<span class="contact-info"><?php echo $record['phone_number'] ?></li>

<?php endif ?>
<?php endforeach ?>

</ul>

<?php endforeach ?>


So in this code, as it loops through the participants records, it will only output records where the category field is equal to the num of the category record currently being outputted.

Give this a try and let me know if you run into any issues.

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] Display Sub-categories in a Simple List

By cjpritchard - November 30, 2010

Hi Jason,

Thanks very much for your help. Yes, I have a category field in the participants table and it's using the num field from the category section.

Here's the page now: http://www.tucg.ca/participants2.html

Thanks again,
Chris

Re: [cjpritchard] Display Sub-categories in a Simple List

By Jason - November 30, 2010

Hi Chris,

Replace the number 35 with this:
<?php echo count($participantsRecords);?>

This will output the number of records stored in $participantsRecords.

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] Display Sub-categories in a Simple List

By cjpritchard - November 30, 2010

Perfect, thanks very much.

Chris