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 statement not working (part 2)

May 11, 2012 4:32am

Subscribe [2]
  • #1 / May 11, 2012 4:32am

    Wayne Smallman

    176 posts

    This question is be related to a resolved thread.

    I’ve just gone in to ExpressionEngine to finish off the work I began, using the conditional statement ( http://ellislab.com/forums/viewthread/216922/#1002267 ) I had problems with yesterday and while it appeared to be working, it’s actually not — it’s evaluating to true every single time, no matter what the values of the fields are, whether they have data in them or not.

    I’ve even swapped out the code for PHP and it’s still doing the same thing:

    <?php
    
    $title_shop = '{embed:title_shop}';
    $link_shop = '{embed:link_shop}';
    
    if (isset($title_shop) && isset($link_shop)): ?>html…<?php endif; ?>

    I only want the HTML to appear if there is data in both the variables.

    Any ideas?

  • #2 / May 11, 2012 5:14am

    mark186282

    290 posts

    if you define the variable, then it will evaluate as true for “isset()”

    In your example, if the title_shop and link_shop are empty, then the php variables are set, but just have no content in them.

    $title_shop = ''; // isset($title_shop) will evaluate as "true"

    What you may want to do is this:

    <?php
    
    $title_shop = '{embed:title_shop}';
    $link_shop = '{embed:link_shop}';
    
    if ($title_shop != '' && $link_shop != '') { ?>html…<?php } ?>
  • #3 / May 11, 2012 5:20am

    Wayne Smallman

    176 posts

    if you define the variable, then it will evaluate as true for “isset()”

    In your example, if the title_shop and link_shop are empty, then the php variables are set, but just have no content in them.

    Hey, thanks for the reply, but that just stops the whole thing from working.

    I use isset() like the way I have it all the time, which works perfectly fine. A variable can be assigned to another that potentially has no value.

  • #4 / May 11, 2012 5:25am

    mark186282

    290 posts

    I totally agree that you can assign a variable to another, but if “{embed:title_shop}” is empty… the code would be processed as:

    $title_shop = ‘’;

    If you are looking to see if the PHP variable $title_shop “is set” (has been defined) - it will ALWAYS be evaluated as “true” (even if the EE variable embed:title_shop has not been set)

    If you are looking to see if the EE variable embed:title_shop is empty… then you would use the PHP code $title_shop != ‘’

  • #5 / May 11, 2012 5:39am

    Wayne Smallman

    176 posts

    I totally agree that you can assign a variable to another, but if “{embed:title_shop}” is empty… the code would be processed as:

    $title_shop = ‘’;

    If you are looking to see if the PHP variable $title_shop “is set” (has been defined) - it will ALWAYS be evaluated as “true” (even if the EE variable embed:title_shop has not been set)

    If you are looking to see if the EE variable embed:title_shop is empty… then you would use the PHP code $title_shop != ‘’

    All I want is for a conditional statement to only trigger if the aforementioned values have data in them. I don’t care too much how it’s accomplished.

  • #6 / May 11, 2012 5:55am

    mark186282

    290 posts

    Alright, give this a spin (I just tested this on one of my installs) - keep PHP out of the picture as much as possible in your template

    {if '{embed:title_shop}' AND '{embed:link_shop}'}These both have content{/if}
  • #7 / May 14, 2012 10:30am

    Dan Decker

    7338 posts

    Hi Wayne,

    Thanks for your question!

    According to ExpressionEngine’s Parse Order, Mark’s example should work as you expect.

    Can you verify you are seeing the results you want?

    Cheers,

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

ExpressionEngine News!

#eecms, #events, #releases