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.

php in EE conditional statement

October 18, 2011 2:42pm

Subscribe [6]
  • #1 / Oct 18, 2011 2:42pm

    davedriesmans

    14 posts

    why does

    <?php 
         $text_counter=0;
    ?>
    
    {embed="global/header" loc="homepage"}
    {exp:channel:entries channel="homepage_layout" status="open" limit="8" }
            {media_type}
            {if media_type == "text"}
                    <?php 
                         $text_counter++;
                        echo $text_counter;
                     ?>
            {/if}
    {/exp:channel:entries}

    has this output

    text 1
    image
    text 3
    text 4
    image

    and not

    text 1
    image
    text 2
    text 3
    image

    or even (if php decides to ignore the EE statements)
    text 1
    image 2
    text 3
    text 4
    image 5

    anyone?

  • #2 / Oct 18, 2011 5:30pm

    Barrel

    92 posts

    That’s so strange! I just tried loading this myself and can’t figure out how to fix it. I wonder if it has something to do with the way PHP gets parsed after all of the EE tags are called (in output mode)...

  • #3 / Oct 19, 2011 6:24am

    ahmad saad

    364 posts

    i tested it and i see that’s Parsing order problem

    and the only way it’s work like this:

    <?php 
         $text_counter=0;
    ?>
    {exp:channel:entries channel="homepage_layout" limit="8" }
          {media_type}  <?php $media_type="{media_type}";?>
            <?php if($media_type == "text") {$text_counter++; echo $text_counter;}?>
                    
    
    {/exp:channel:entries}
  • #4 / Oct 19, 2011 11:32am

    Mark Bowen

    12637 posts

    You have an {if media_type} conditional in your code above and you have the PHP increment inside that code so if the media type isn’t text then this PHP increment is never going to fire.

    What exactly is your end goal here though?

    Are you just trying to get a count for each item or only have an incrementing count for text items?

    If we know more about what you’re trying to do here then that might help in an answer for you.

    Thanks,

    Mark

  • #5 / Oct 19, 2011 11:40am

    davedriesmans

    14 posts

    > You have an {if media_type} conditional in your code above and you have the PHP increment inside that code so if the media type isn’t > text then this PHP increment is never going to fire.

    well, it does not do the echo but does do the counting, see my output and i think that’s a bug?

    what i want to get:
    i want to count each media type and pass on that count as a parameter to an embed template to use it in the offset.
    in the design: 8 boxes where user can define the media type, but we can’t have the same content when the media type is the same

  • #6 / Oct 19, 2011 12:04pm

    Mark Bowen

    12637 posts

    Hi davedriesmans,

    If you are after a simple count you can use the {count} variable as shown below :

    {exp:channel:entries channel="homepage_layout" status="open" limit="8" }
            {media_type} - {count}
    {/exp:channel:entries}

    Does that help here at all?

    Thanks,

    Mark

     

  • #7 / Oct 19, 2011 2:09pm

    davedriesmans

    14 posts

    no, i need a count in the IF…

  • #8 / Oct 19, 2011 2:32pm

    davedriesmans

    14 posts

    could solve it with ahmad saad’s solution, thanks!
    also many thanks for the other people helping here

    (but still think there is an EE bug with php&conditionals; though)

  • #9 / Oct 19, 2011 3:19pm

    Robin Sowell

    13255 posts

    I’d go wtih ahmad’s solution as well- mixing EE and php conditionals can get confusing fast.  This wiki entry may help?  But it’s definitely easier to follow if you go to php for the comparison as the parsing is easier to ... well, parse.

  • #10 / Oct 19, 2011 3:21pm

    davedriesmans

    14 posts

    well, moderator, can you tell me why he did what he did?
    i mean it’s maybe confusing for EE… 😉

  • #11 / Oct 20, 2011 11:20am

    Mark Bowen

    12637 posts

    This is due to parsing order as mentioned by Ahmad above.

    What you were initially trying to do would require both Input and Output parsing on the same template which is impossible to do but doing things in Ahmads way by using a PHP conditional instead will get around this completely.

    Does that help at all?

    Thanks,

    Mark

    P.S. Thanks Ahmad for the help here!

  • #12 / Oct 24, 2011 5:48pm

    Robin Sowell

    13255 posts

    Sorry- just to elaborate a bit on Mark- it’s treating the EE conditional as an advanced conditional- which is parsed late.  So basically?  The php runs each time, but it is only displayed when the EE conditional is a match.

    Which is something to be very careful of if you’re putting tags in conditionals and don’t want them to run for optimization purposes!

  • #13 / Nov 15, 2011 11:20am

    e-man

    1816 posts

    To get the lowdown on parsing order, check @low ‘s slides from his EECI2011 talk: Parse Order Pro:
    http://www.slideshare.net/lodewijkschutte/parse-order-pro
    Highly recommended!

  • #14 / Nov 15, 2011 11:41am

    Robin Sowell

    13255 posts

    Ha- I didn’t have the link handy or I’d have added it as well.  Thanks e-man.  That is a good one.

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

ExpressionEngine News!

#eecms, #events, #releases