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.

Conditional

November 29, 2011 7:55am

Subscribe [2]
  • #1 / Nov 29, 2011 7:55am

    Hi. I have a huge conditional that I am using to refine results.

    If I use this conditional with an if:else, regardless if there are any results it displays both.

    See code below:

    {if segment_2 == "" XOR (segment_2 == "africa" AND continent == "Africa") XOR (segment_2 == "antarctica" AND continent == "Antarctica") XOR (segment_2 == "asia" AND continent == "Asia") XOR (segment_2 == "australia" AND continent == "Australia") XOR (segment_2 == "europe" AND continent == "Europe") XOR (segment_2 == "north_america" AND continent == "North America") XOR (segment_2 == "south_america" AND continent == "south_america") XOR (segment_2 == "-500" AND tour_price <= "500") XOR (segment_2 == "500-1000" AND (tour_price >= "500" AND tour_price <= "1000")) XOR (segment_2 == "1000-2000" AND (tour_price >= "1000" AND tour_price <= "2000")) XOR (segment_2 == "2000+" AND tour_price >= "2000") XOR (segment_2 == "easy" AND tour_exertion == "Easy") XOR (segment_2 == "hard" AND tour_exertion == "Hard")}
    
    ...content to show if…
    
    {if:else}
    Ooops, nothing available
    {/if}

    The ‘Ooops, nothing available’ displays once when there are results, and twice when there arent…

    Can anyone see a reason why? Is my conditional way too long? possibly a better way to achieve this?

    Thanks

  • #2 / Nov 29, 2011 8:28pm

    Sean C. Smith

    3818 posts

    Hi Chris Davies,

    Wow, that is a really long conditional. Have you tried simplifying the conditional so that it works with a limited set and then expanding it until you get to where you want it to be?

    Also Could you share more of your template? This would help me get a better handle on what you are trying to do and thus see if there is a better way to achieve it.

    Sean

  • #3 / Nov 30, 2011 2:30am

    Hi Sean.

    I am using this to refine results. I have different links like:

    <li {if segment_2 == "africa"}class=“selected”{/if}>Africa</li>

    If the workshop country is Africa then it displays.

    I am not entirely clear what you mean by a limited set? Could you possibly show me an example? Is there a better way to achieve this?

    Thanks

  • #4 / Dec 02, 2011 8:48pm

    Sean C. Smith

    3818 posts

    Chris,

    What I mean by a more limited set is instead of having a very long conditional, using a shorter conditional to start with and adding to it until it stops working so that you would find the problem.

    So instead of:

    {if segment_2 == "" XOR (segment_2 == "africa" AND continent == "Africa") XOR (segment_2 == "antarctica" AND continent == "Antarctica") XOR (segment_2 == "asia" AND continent == "Asia") XOR (segment_2 == "australia" AND continent == "Australia") XOR (segment_2 == "europe" AND continent == "Europe") XOR (segment_2 == "north_america" AND continent == "North America") XOR (segment_2 == "south_america" AND continent == "south_america") XOR (segment_2 == "-500" AND tour_price <= "500") XOR (segment_2 == "500-1000" AND (tour_price >= "500" AND tour_price <= "1000")) XOR (segment_2 == "1000-2000" AND (tour_price >= "1000" AND tour_price <= "2000")) XOR (segment_2 == "2000+" AND tour_price >= "2000") XOR (segment_2 == "easy" AND tour_exertion == "Easy") XOR (segment_2 == "hard" AND tour_exertion == "Hard")}

    start with

    {if segment_2 ==""}

    Then add change to

    {if segment_2 == "" XOR (segment_2 == "africa" AND continent == "Africa")}

    and keep going like that until you find the part that the conditional breaks.

    Sean

  • #5 / Dec 04, 2011 4:49am

    Hi Sean.

    I have tried like you suggested. It appears its nothing to do with the length.

    Somehow now, if there are results it displays the {if:else} part once… (no results) and if there are no results it displays it twice… (no results no results)

    Here is the code for the entire section:

    <ul>
    
    {exp:channel:entries channel="tours" limit="10" orderby="date" sort="asc"}
    
    {tour_date}
    
    {if segment_2 == "" XOR (segment_2 == "africa" AND continent == "Africa") XOR (segment_2 == "antarctica" AND continent == "Antarctica") XOR (segment_2 == "asia" AND continent == "Asia") XOR (segment_2 == "australia" AND continent == "Australia") XOR (segment_2 == "europe" AND continent == "Europe") XOR (segment_2 == "north_america" AND continent == "North America") XOR (segment_2 == "south_america" AND continent == "south_america") XOR (segment_2 == "-500" AND tour_price <= "500") XOR (segment_2 == "500-1000" AND (tour_price >= "500" AND tour_price <= "1000")) XOR (segment_2 == "1000-2000" AND (tour_price >= "1000" AND tour_price <= "2000")) XOR (segment_2 == "2000+" AND tour_price >= "2000") XOR (segment_2 == "easy" AND tour_exertion == "Easy") XOR (segment_2 == "hard" AND tour_exertion == "Hard") XOR (segment_2 == "2013" AND '{option_value format="Y"}' == "2013")}
    
        <li>
            
        <div class="tours_summary_container">
        
            <div class="tours_summary_title_price">
                
                <div class="tours_summary_title"><h5><a href="http://{title_permalink=tours/view}">{title}</a></h5><p></div><br />
                <div class="tours_summary_price"></p><h5>£{tour_price}</h5><p></div><br />
            <br />
            </div> <!-- End of .tour_summary_title_price --><br />
            <br />
            <div class="tour_thumbnail"><br />
            <br />
                {tour_images limit="1"}<br />
                    {exp:ce_img:pair src="{tour_image_file}" width="100" height="100" crop="yes"}<br />
                    <a href="http://{title_permalink=tours/view}">{made}</a><br />
                    {/exp:ce_img:pair}<br />
                {/tour_images}<br />
            <br />
            </div> <!-- End of .tour_thumbnail --><br />
            <br />
            <div class="tours_summary_excerpt"><br />
            <br />
                {exp:char_limit total="300"}<br />
                {tour_excerpt}<br />
                {/exp:char_limit}<br />
                <strong><a href="http://{title_permalink=tours/view}">more…</a></strong><br />
            <br />
            </div> <!-- End of .tours_summary_excerpt --><br />
                            <br />
        </div> <!-- End of .tours_summary_container --><br />
        <br />
        <div>/div><br />
        <br />
        </li></p>
    
    <p>{if:else}<br />
    No Results<br />
    {/if}</p>
    
    <p>{/tour_date}</p>
    
    <p>{/exp:channel:entries}</p>
    
    <p></ul>

    Can you see anything wrong here?

    Thanks

  • #6 / Dec 06, 2011 10:39pm

    Dan Decker

    7338 posts

    Hi Chris,

    I’m assuming that {tour_date} is a Matrix field? Is continent a separate field in that channel? I’m thinking if you break the conditional up to the parts that can execute outside of the {tour_date} field and then those that require information in the {tour_date} field:

    <ul>
    
    {exp:channel:entries channel="tours" limit="10" orderby="date" sort="asc"}
    
    {if segment_2 == "" 
     XOR (segment_2 == "africa" AND continent == "Africa")
     XOR (segment_2 == "antarctica" AND continent == "Antarctica")
     XOR (segment_2 == "asia" AND continent == "Asia")
     XOR (segment_2 == "australia" AND continent == "Australia")
     XOR (segment_2 == "europe" AND continent == "Europe")
     XOR (segment_2 == "north_america" AND continent == "North America")
     XOR (segment_2 == "south_america" AND continent == "south_america")}
    {tour_date}
    {if (segment_2 == "-500" AND tour_price <= "500")
     XOR (segment_2 == "500-1000" AND (tour_price >= "500" AND tour_price <= "1000"))
     XOR (segment_2 == "1000-2000" AND (tour_price >= "1000" AND tour_price <= "2000"))
     XOR (segment_2 == "2000+" AND tour_price >= "2000")
     XOR (segment_2 == "easy" AND tour_exertion == "Easy")
     XOR (segment_2 == "hard" AND tour_exertion == "Hard")
     XOR (segment_2 == "2013" AND '{option_value format="Y"}' == "2013")}
    <li>
            
        <div class="tours_summary_container">
        
            <div class="tours_summary_title_price">
                
                <div class="tours_summary_title"><h5><a href="http://{title_permalink=tours/view}">{title}</a></h5><p></div><br />
                <div class="tours_summary_price"></p><h5>£{tour_price}</h5><p></div><br />
            <br />
            </div> <!-- End of .tour_summary_title_price --><br />
            <br />
            <div class="tour_thumbnail"><br />
            <br />
                {tour_images limit="1"}<br />
                    {exp:ce_img:pair src="{tour_image_file}" width="100" height="100" crop="yes"}<br />
                    <a href="http://{title_permalink=tours/view}">{made}</a><br />
                    {/exp:ce_img:pair}<br />
                {/tour_images}<br />
            <br />
            </div> <!-- End of .tour_thumbnail --><br />
            <br />
            <div class="tours_summary_excerpt"><br />
            <br />
                {exp:char_limit total="300"}<br />
                {tour_excerpt}<br />
                {/exp:char_limit}<br />
                <strong><a href="http://{title_permalink=tours/view}">more…</a></strong><br />
            <br />
            </div> <!-- End of .tours_summary_excerpt --><br />
                            <br />
        </div> <!-- End of .tours_summary_container --><br />
        <br />
        <div>/div><br />
        <br />
        </li><br />
    {/if}<br />
    {/tour_date}<br />
    {/if}<br />
    {if no_results}<br />
    No Results<br />
    {/if}<br />
    {/exp:channel:entries}

     

  • #7 / Dec 09, 2011 3:28am

    Hi Dan, thanks for your reply. Still no luck.

    Even if I break the conditional down to:

    {exp:channel:entries channel="tours" limit="10" orderby="date" sort="asc" dynamic="no"}
    
    {if segment_2 == "" XOR (segment_2 == "africa" AND continent == "Africa")}
    
        <li>
    
     content
    
        </li>
    
    {if:else}
    no results
    {/if}
    
    {/exp:channel:entries}
    
    </ul>

    It still displays two ‘no results’.

    I have figured out that it is creating a ‘no results’ for each <li>.

    In my test page currently I have two <li> being created with the tag pair. Which is why it displays two if there are no results, and one of there is one result.

    I need it to only display ‘no results’ if there are no results and only once.

    Any ideas? Do I need to move the example above about a bit?

    Thanks

  • #8 / Dec 12, 2011 2:30pm

    Dan Decker

    7338 posts

    Hi Chris,

    The Conditional is working correctly I must say. The reason being that there is nothing in your {exp:channel:entries} tag that *isn’t* returning results. However, for each result, the *internal* conditionals are failing, and you are seeing “no results” based on that. Once for each entry that was returned because:
    1) The entry met the criteria of the {exp:channel:entries} tag
    2) the *content* of that entry did not meet the criteria of the “internal” condition.

    Does that make sense?

    Cheers,

  • #9 / Dec 12, 2011 3:21pm

    Hi Dan. Ah that makes sense… but how can I solve it?

  • #10 / Dec 13, 2011 11:02pm

    John Henry Donovan

    12339 posts

    Hi Chris,

    what type of custom field is continent?

    Is there a value and a label? As in africa is the value and Africa is the label?

    If so you could do something like

    {if segment_2 == "" XOR (segment_2 == "{continent}{option}{/continent}")}

    it would depend what kind of field continent is but do you see where I am coming from?

     

  • #11 / Jan 16, 2012 8:22am

    Hi. I am still having issues with this.

    Maybe I am going about this the wrong way?

    I simply want to display the entries that comply with a segment of the URL.

    For instance, segment_2 says ‘africa’, any entries that have ‘africa’ selected in the ‘continent’ channel field displays. My method above kind of works, it just fails to allow me to display a message when no entries are displayed, which is crucial.

    Can anyone show me how I can complete this? Please!?

    Chris

  • #12 / Jan 18, 2012 9:51am

    Anyone?

  • #13 / Jan 18, 2012 10:08am

    Matt Powell

    42 posts

    Hi Chris

    Sorry, no answer but hoping to help with a little more info…

    Could you actually describe exactly what your conditional is trying to achieve? I can see what the conditionals are saying but in real words what is the query trying to do?

    Are you able to send us a link to a dev site?

    Cheers
    Matt

  • #14 / Jan 18, 2012 10:15am

    Hi Matt.

    Thanks for your reply.

    Currently no dev site online, but I can give you a better idea of what I am trying to achieve.

    I have a channel called ‘tours’. In this channel I have channel fields where I add details about each ‘tour’, like ‘continent’, ‘date’ etc.

    On an index page where I am displaying lists of these ‘tours’, I have some buttons. Each of these buttons has a different option to refine the ‘tours’.

    So for example, a button to refine the continent to Africa would have: domain.com/blah/africa

    What I want my conditionals to do is say, okay, segment 2 says africa, which entries in the channel field ‘continent’ say ‘africa’ and display just those results.

    The problem is, this works to some degree. But if there are no entries with ‘africa’ selected in the field ‘continent’ it just shows an empty page. I need to be able to have a no results message.

    If I add a no results tag, it displays 1 message for each list item in the list.

    I hope this makes sense.

    Any ideas?

  • #15 / Jan 18, 2012 11:59am

    Matt Powell

    42 posts

    Hi

    How about taking your segment conditional outside of the exp:channel bit? So run the if segment first, then run the exp:channel bit after that.

    I have a feeling you are getting results to your query, then running the if, and then running the no results… Which may be causing issues. Mig be worth simplifying right down and testing just with Africa., then build it up and ensure its working at each stage - trying to decipher that long statement all in one go will give you a headache ! ( seems to have already!)

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

ExpressionEngine News!

#eecms, #events, #releases