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.

Need Help with custom date field and limit using conditionals

November 07, 2009 4:13am

Subscribe [3]
  • #1 / Nov 07, 2009 4:13am

    zork

    11 posts

    I have custom date fields for an “Upcoming Events” listing. I am showing current and upcoming events using conditionals. I want to only list the next 5 events that are coming up.  However, as you might have guessed, I’m running into an issue because of the past events in the system.

    The limit parameter takes into account all of the events, including the ones in the past that I don’t want to show. I know a lot of people have solved this problem using the entry_date and the start_on parameters.  This is not an option for me.  I have content editors who have trained to use the custom date field and are not computer savvy. There are also hundred of events already in the system that use the custom date fields.

    Here is my code that works without the limit:

    {exp:weblog:entries weblog="upcoming-events" dynamic="off" orderby="event_start_date" sort="asc" }                         
                            {if event_end_date > current_time || "{event_start_date format="%Y-%m-%d %H:%i"}" >= "{today}" || "{event_end_date format="%Y-%m-%d %H:%i"}" >= "{today}"}  
                                  
                                       {event_start_date format="%m/%d/%y"} - {event_end_date format="%m/%d/%y"}
                                       
    
                                       {title}
                                   
                             {/if}
                   {/exp:weblog:entries}

    Could there be a way to do this with php?

    I tried to set a counter but I got the same results.

    <?php 
    $count = 0;  
    $limit = 4;  
    ?>
    
    
    {exp:weblog:entries weblog="upcoming-events" dynamic="off" orderby="event_start_date" sort="asc" }
    <?php
          if ($count < $limit)
          {
    ?>           
    
    {if "{event_start_date format="%Y-%m-%d %H:%i"}" >= "{today}" || "{event_end_date format="%Y-%m-%d %H:%i"}" >= "{today}"}
            
                {event_start_date format="%m/%d/%y"} - {event_end_date format="%m/%d/%y"}
                
    
                {title}
             
        
       
    <?php
        $count++;
    ?>
    {/if}
    
    
    <?php
      }
    ?>
       
    {/exp:weblog:entries}
  • #2 / Nov 07, 2009 5:50pm

    Gareth Davies

    491 posts

    There’s this extension from Solspace that would probably work for you?

    http://www.solspace.com/software/detail/date_field_filter/

    Otherwise you’d be best off with a custom query.

  • #3 / Nov 07, 2009 7:41pm

    Osze

    42 posts

    It doesn’t work with:
    show_expired=“no” show_future_entries=“yes” limit=“5” ?

  • #4 / Nov 07, 2009 10:12pm

    Gareth Davies

    491 posts

    It doesn’t work with:
    show_expired=“no” show_future_entries=“yes” limit=“5” ?

    It’s a custom field so no, that’s not going to work…

  • #5 / Nov 07, 2009 11:20pm

    zork

    11 posts

    thanks for the replies.  It looks like the extension from Solspace might do the trick, but I’m not happy about purchasing yet another third party extension to do something that EE should be able to do.

    I’m going to try a few other things before I buy it.  I’ll post the results here.  If anybody has a solution or could point me in the right direction, I would appreciate it.

  • #6 / Nov 07, 2009 11:55pm

    cherrypj

    158 posts

    Otherwise you’d be best off with a custom query.

    Gareth mentioned a custom query: he’s right. You can limit the number of results that come back from a Query:

    {exp:query sql="SELECT entry_date FROM exp_weblog_titles ORDER BY entry_date DESC LIMIT 5"}

    Find your custom fields in the exp_weblog_fields table. More on the Query module.

  • #7 / Nov 20, 2009 8:11pm

    zork

    11 posts

    Thanks for the replies.  I tried the SQL Query with some luck.  I’m sure if I knew more about SQL or PHP I could get it working.  I did manage to pull in my dates except I couldn’t get them to format correctly.

    {/exp:query}
                   
    {exp:query sql="
    SELECT field_id_20, field_id_21 
    FROM exp_weblog_data 
    ORDER BY field_id_20 
    ASC 
    LIMIT 10"}
    
    {field_id_20} - 
    {field_id_21}
    
    
              
    {/exp:query}

    After spending a some time with it, I decided to drop the $8 on the Solspace Date Field Filter.  Now everything is working the way I want.  This code will list the next four upcoming events, including events that currently going on:

    <!-- uses fresh variables {today} and Solspace Date Field Filter -->
                {exp:weblog:entries weblog="upcoming-events" date_field="event_start_date" date_field_start="{today}" orderby="event_start_date" sort="asc" limit="4"}                        
                            {if event_end_date > current_time || "{event_start_date format="%Y-%m-%d %H:%i"}" >= "{today}"}  
                                  
                                       {event_start_date format="%m/%d/%y"} {if event_end_date}- {event_end_date format="%m/%d/%y"} {/if}
                                       
    
                                       {if website-address}
                                           <a class="event-link">{title}</a>
                                       {if:else}{title}
                                       {/if}
                                   
                             {/if}
                   {/exp:weblog:entries}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases