Hide empty categories

11 posts by 3 authors in: Forums > CMS Builder
Last Post: February 28, 2011   (RSS)

Hello,

I currently have a page that lists all of my categories in a tree structure.

I would like to modify this so that empty categories (i.e. those with no listings) are hidden.

Could someone please tell me how to go about this?

Thanks,
Greg

Re: [gversion] Hide empty categories

Hi Greg,

Here's an excerpt from my CMSB Cookbook http://www.thecmsbcookbook.com that should set you in the right direction.

UNDERSTANDING AND USING IF STATEMENTS

...You can use if statements to test for a particular condition. Say to test whether the field “your_field” is empty:

In a single record viewer:

<?php if ($your_tableRecord['your_field']): ?> ...your code...
<?php endif ?>

Or in a multi record viewer:

<?php foreach ($your_tableRecords as $record): ?>
<?php if ($record['your_field']): ?> ...your code...
<?php endif ?>
<?php endforeach; ?>


Hope that gives you a starting point.

If statements are a very powerful tool and there's a lot more information on how to use them to achieve your goals in the Cookbook.

Best,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gversion] Hide empty categories

By Jason - February 24, 2011

Hi Greg,

I'm assuming that you're storing your categories in one section, and your listings in another. Is that right?

In that case, you'll need to figure out how many listings is in each category before you begin outputting them. If you could provide some more information on your sections (ie, does a listing have only one, or multiple categories? What is the name of the category field in the listings section?) and also attach the .php file you're working with, I can try to give you a more specific example of how to do this.

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: [gversion] Hide empty categories

By Jason - February 25, 2011

Hi Greg,

In order to do this, there are a couple things we need to do. First, we need to get all of our listings and then figure out how many listings are in each category. We'll use this code under where we select our categories:

NOTE: this example assumes your using the category's num field as the value.


// get listings records
list( $listingsRecords, $listingsMetaData ) = getRecords(array(
'tableName' => 'listings',
'allowSearch' => false,
));

//talling the number of listings for each category
$categoryNumToListingsCount = array();

foreach($listingsRecords as $listing){

@$categoryNumToListingsCount[ $listing['category'] ] ++;
}


This code creates an array using category numbers as an index and counts up how many listings use each category.

Next, where we're outputting our categories, we can check how many listings that category has. If it doesn't have any, we'll use the "continue" command to skip to the next category:
(change in red)


<?php foreach ($categoryRecords as $record): ?>
<?php if(!@$categoryNumToListingsCount [$record['num']]) { continue; } ?>

<?php echo $record['_listItemStart'] ?>
<a href="searchResults.php?categories=<?php echo $record['num'] ?>"><?php echo $record['name'] ?></a>
<?php echo $record['_listItemEnd'] ?>
<?php endforeach; ?>


Hope this helps get you started.
---------------------------------------------------
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] Hide empty categories

Hi Jason,

Thanks very much for your help.

I have inserted the code you suggested and after resolving a small error message, I am now not seeing any categories at all :)

I think it might be something to do with the "num" value you mentioned... I am using the "num" value as the "Record Number".

I have attached the file that I am using for you to take a look at if that's OK.

Thanks again,
Greg
Attachments:

allcats.php 2K

Re: [gversion] Hide empty categories

By Jason - February 28, 2011

Hi Greg,

In your listings section, what are you storing as the value of your category field? You would need to use 2 different fields, one for the label (what you see) and one for the value (what is actually stored in the database).

The code example I gave you assumed that you're using the "num" field from the category section as the value for your category list field (in the listing section).

If that's not the case, you'll need to change the code to match how you're storing things in the database.

Confirm how you have your category field set up in the listing section and let me know if you run into any problems.

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] Hide empty categories

Hi Jason,

Thank you for your message.

I am using "name" as the value of the 'category' field and "breadcrumb" for the label.

I have therefore tried changing "num" to "name" but still not seeing any categories...

Is there another change required somewhere?

Thanks again,
Greg

Re: [gversion] Hide empty categories

By Jason - February 28, 2011

Hi Greg,

Even though you've changed the value to "num", those records are still storing "name" as their value in the database. What you'll need to do is go through your records and re-select a category. That way the record will have the "num" stored.

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] Hide empty categories

Hi Jason,

Am I right in thinking that we have to use the "num" value as you are counting the number of listings?

I therefore can't just change the code you provided:

<?php if(!@$categoryNumToListingsCount [$record['num']]) { continue; } ?>

to be as follows:

<?php if(!@$categoryNumToListingsCount [$record['name']]) { continue; } ?>


Thanks,
Greg