ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Display catergory names only if entries exist

January 29, 2008 4:03am

Subscribe [4]
  • #1 / Jan 29, 2008 4:03am

    peak2peak

    18 posts

    I am trying to create three seperate class schedules based on three different locations. The approach I am using almost works but I am stumped on how to get it to do exactly what I need. This may not be the best approach so I am open to any other suggestions that may work. Hopefully I have provided enough information to make sense.

    Background:

    There are 3 business locations that offer classes. Each location will have a unique class schedule.


    Objective:

    Display 3 seperate schedules; one for each location (i.e., East Location, South Location, North Location).


    Data Breakdown:

    Categories are courses (i.e.,BEGINNING, INTERMEDIATE, ADVANCED, etc.)

    The weblog is made up of each individual class offered AND the LOCATION. Some classes may not be offered at all locations.

    example of class data:

    220 TUE 11:00-12:00 SOUTH LOCATION
    301 MON 12:20-1:45 NORTH LOCATION


    Problem:

    I only want to display the category names for classes being offered at the location I am displaying the scheudle for.

    For example, the North Location only offers an ADVANCED course. When I display the North Location schedule, I would like it to look like this:

    ADVANCED
    301 MON 12:20-1:45

    What I am getting is:

    BEGINNING
    INTERMEDIATE
    ADVANCED
    301 MON 12:20-1:45

    Note that there is a BEGINNING and INTERMEDIATE class being offered at other locations. So the code below produces the above results correctly, I just need to take it one step farther.

    The code:

    {exp:weblog:category_archive weblog="schedule" style="linear" show_empty="no"}
    
    {categories}
    
    {!-- Display the category name --}
    {category_name}
    
    {!-- Display all weblog entries for this category --}
    {exp:weblog:entries weblog="schedule"  category="{category_id}"}
    
    {!-- Display classes for North Location only. All classes at North Location range between 301-399. --}
    {if {class-number} > 300  && {class-number} < 400}
    
    .
    .
    display class info
    .
    .
    
    {/if}
    {/exp:weblog:entries} 
    {/categories}
    {/exp:weblog:category_archive}

    NOTE: show_empty=“no” works if no class is being offered at any locations. There is a BEGINNING and INTERMEDIATE class being offered at other locations so the category is not empty and thus the extra categories will be displayed.

  • #2 / Jan 29, 2008 5:02am

    tbritton

    714 posts

    I think that you may be wanting to use the exp:weblog:entries tag and its categories tags rather than the archives tags. But that is more an intuition than an understanding of why you are using the exp:weblog:category_archive tags for this purpose. Perhaps the exp:weblog:categories tag would offer more flexibility for your needs.

    I will try some ideas and get back to you. I think I’m grasping what you are after (former professor!)

    Terry

  • #3 / Jan 31, 2008 7:03pm

    peak2peak

    18 posts

    I was hoping I wouldn’t have to use any PHP but I did find a solution. I went ahead and used the exp:weblog:entries tag as Terry suggested but still had the same results. Here is the solution I used.

    {exp:weblog:categories weblog="schedule" style="linear" show_empty="no"}
    
    {!-- Display all weblog entries for this category --}
    {exp:weblog:entries weblog="schedule"  category="{category_id}"}
    
    {!-- Display the category name only once if any entries exist for the North Location --}
    <? 
    if({location}=='north location' && $count==0) 
    {
    {echo "{category_name}";}
    $count++;
    }
    ?>
    
    {!-- Display classes for North Location only. All classes at North Location range between 301-399. --}
    {if {class-number} > 300  && {class-number} < 400}
    
    .
    .
    display class info
    .
    .
    
    {/if}
    {/exp:weblog:entries} 
    
    <? $count = 0; ?> // reset counter for next loop iteration
    
    {/exp:weblog:categories}
  • #4 / Jan 31, 2008 7:50pm

    tbritton

    714 posts

    Beautiful! Thank you!

    I was going to get to this tonight, but you saved me the trouble AND supplied me with a solution I wouldn’t have tried! I didn’t know you could simply include the $count string variable like that.

    Terry

  • #5 / Feb 08, 2008 11:20am

    ASLFilms

    15 posts

    All,

    I wasn’t sure where to post this, but since this thread addresses printing specific categories.  IN my case, because EE did not have a way for me to determine whether a particular category had an entry in it or not.  I did not want to print the categories that were empty for that particular issue (newsletter).  EE for some reason sees that it’s not empty because there are other issues that use that particular category.

    Hence, I had to write up a fix to print only those category headings when there exist an entry for that particular issue.  I thought I’d share this with everybody here.

    <?php $count = 0; ?>

    {exp:weblog:categories weblog="news" style="linear" show_empty="no" show="not 23"}
    {exp:weblog:entries weblog="issues" entry_id="{segment_3}” disable=“member_data|trackbacks|pagination” rdf=“off” show_expired=“yes” }
    {reverse_related_entries status="open"}
    {exp:query sql="SELECT count(*) as pcount FROM exp_weblog_titles ewt JOIN exp_category_posts ecp ON ewt.entry_id = ecp.entry_id AND ecp.cat_id = {category_id} AND ecp.entry_id = {entry_id} ORDER BY ecp.entry_id”}
    <?php
    if ($count == 0 && {pcount} > 0 )
    {
    echo "<dt>{category_name}</dt>”;
          $count++;
    }
    ?>
    {/exp:query}
    {/reverse_related_entries}
    {/exp:weblog:entries}

    <?php $count = 0; ?>
    {embed="lib/news" catid="{category_id}” relid=”{segment_3}”}
    {/exp:weblog:categories}

    I set up this template to run PHP in an output mode.  If the pcount for a particular category is 0, it means that the category for that particular issue does not have an entry. 

    Modify this as you see fit.  😊

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases