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.

Issues with Opening Hours using Matrix

January 05, 2012 12:19pm

Subscribe [2]
  • #1 / Jan 05, 2012 12:19pm

    OwensDDB

    42 posts

    I’m developing a site for a shopping centre in which each shop has separate opening hours. They appear on a list, and on that list I want to show the opening hours for that specific day. This is currently set up in the backend with two sets of Matricies, one for normal hours, the other for specific days (mostly for over xmas when they have random hours), set up like thus;

    {opening_hours}
        {day} (text field)
        {hours} (text field)
    {/opening_hours}

    {specific_opening_hours}
        {day} (date field)
        {hours} (text field)
    {/specific_opening_hours}

    The code I have used is

    {specific_opening_hours}
        {if "{day format='%Y%m%d'}" == "{current_time format='%Y%m%d'}" }
           {if hours=="Closed"}
              {hours}
           {if:else}
              {hours}
          {/if}
        {if:elseif "{day format='%Y%m%d'}" != "{current_time format='%Y%m%d'}" } 
            {opening_hours}
                {if "{current_time format='%l'}" == "{day format='%l'}"}{hours}{/if}
            {/opening_hours}                                      
        {/if} 
    {/specific_opening_hours}

    You can see what is should look like, and also how the above code renders.

    Basically, unless a specific opening hours is that specific day, it should display the regular opening hours. Both work independently, but as shown here, is displays BOTH the specific and daily hours, not either / or. Where is the flaw in my logic?

    Many thanks

  • #2 / Jan 05, 2012 2:23pm

    For clarification, you’ve got a channel for stores that contains two matrix fields, one called specific_opening_hours, the other opening_hours, correct?

    The first potential problem I see is that you’re applying “format=” to a text field.  This code might solve that problem:

    <pre><code>{specific_opening_hours}
      {if "{day format='%Y%m%d'}” == “{current_time format='%Y%m%d'}” }
        {if hours=="Closed"}
          {hours}
        {if:else}
          {hours}
        {/if}
      {if:elseif "{day format='%Y%m%d'}” != “{current_time format='%Y%m%d'}” }
          {opening_hours}
            {if "{current_time format='%l'}” == “{day}”}{hours}{/if}
          {/opening_hours}                         
      {/if}
    {/specific_opening_hours}</code></pre>

    That said, I see you’re nesting a matrix within a matrix, and both have columns with the same name.  I’m not sure if it’s possible to do this or not.  If it isn’t, you might need to include an embed to contain the {opening_hours} tag pair.

  • #3 / Jan 06, 2012 7:16am

    John St-Amand

    865 posts

    I agree with Nick - the nesting of a matrix within a matrix is likely part of the issue - I’ve run into that before.  For clarity and consistently, you may want to embed both sets of times and just use the outside matrix tag pair where you have it now to do the date comparison.

    I could be misreading it but I think the root of the problem is that you don’t yet have a search condition scanning through the matrix to select only the row that applies to TODAY.  Because it’s not limited to today this way, both conditions applied to the specific hours are being met from at least one row within each matrix.  So it is simultaneously true that today’s date is equal to a special date somewhere in the matrix AND it is also true that today’s date is not equal to a special date in the matrix.  And so because you are using if and if:elseif - both conditions having been met means they both display.  So really what you need is to add a search parameter to the opening special_opening_hours tag to look for TODAY and thereby only do a comparison of the matrix rows that match today.  Then the result of the if statements would not both be met at the same time.

    Hope that makes sense.

  • #4 / Jan 10, 2012 12:09pm

    OwensDDB

    42 posts

    Thanks for the feedback guys

    I’ve started another approach, but am no more successful. My new approach is having one single Matrix table, with two columns. Then, having a second channel called “Bank Holidays” which has a single date field. In this channel, I enter every bank holiday date.

    Then, what I want to do is first ask if today is a bank holiday by checking the current date against the channel of Bank Holiday dates. After this, I want to display the relevant field from the matrix in my Store channel. I flip through the opening hours Matrix until I find one that says “Bank Holidays” and display the corresponding hours.

    Then, I run the same Matrix again, if it matches the day of the week (as specified by current_time) it will display the day of the week’s opening hours.

    {if "{current_time format='%Y%m%d'}" == "{embed="includes/bank_holiday"}"}             
           {opening_hours}
              {if "{day}" == "Bank Holiday"}   
                      {hours}                        
              {/if}  
           {/opening_hours}
    {/if} 
     
    {opening_hours}
       {if "{day}" == "{current_time format='%l'}" }              
            {hours} 
       {/if}
    {/opening_hours}

    The bank_holiday embed is.

    {exp:channel:entries channel="bank_holiday_date" status="not closed" dynamic="no"}
     {bank_holiday_date format='%Y%m%d'}
    {/exp:channel:entries}

    Then I run it, and Expression Engine freaks out big time.

    Any thoughts?

     

  • #5 / Jan 11, 2012 6:34am

    OwensDDB

    42 posts

    I have taken onboard some of John’s advice and am using search in the query.

    {exp:channel:entries channel="store" limit="1" sort="desc" orderby="title" status="not closed"}                                   
        {opening_hours search:day="=Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday"}
            {if "{day}" == "{current_time format='%l'}"}
                {hours}
            {/if}
        {/opening_hours}
    {/exp:channel:entries}

    This all works fine. However.

    How to I go from here to saying “If today is NOT a bank holiday, show the above” and “If today IS a bank holiday, show the bank holiday hours”

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

ExpressionEngine News!

#eecms, #events, #releases