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.

On PHP conditionals with a variable test…odd output!

August 27, 2009 4:17pm

Subscribe [2]
  • #1 / Aug 27, 2009 4:17pm

    octavianmh

    69 posts

    First off, thanks in advance, you guys all rock. 

    Here’s my issue.. I’m trying to do something seemingly simple.. write a template that will display the contents of a weblog (an artists “works”), in a grid, but ONLY works that fulfill a condition (are “Available”).  I’ve tried this a number of different ways, but seem to keep running into an issue around php / template processing order.  The results are simply not consistent.

    Here’s my code:

    <h3>AVAILABLE WORKS</h3>
    <p><?php <br />
    $counter = 0;<br />
    ?><br />
    <table border="0" cellpadding="6" align="center"><tr><br />
    {exp:weblog:entries weblog="titus_works" sort="asc"}</p>
    
    <p>    {if work_status == "Available"}<br />
            <td valign="top"><a href="http://{url_title_path=" target="_self" rel="noopener">{work_thumbnail}</a></td><br />
            <?php<br />
                echo $counter;<br />
                if (($counter % 5) == 0) {<br />
                    echo '</tr></table><table cellpadding="6" align="center"><tr>';<br />
                }<br />
                $counter++;<br />
            ?><br />
        {/if}<br />
    {/exp:weblog:entries}<br />
    </tr></table>

    Trouble seems to be centering around the “if work_status” line..it it incrementing “counter” on every loop, NOT just on the loops where the work is “Available”.

    Is there something dumb i’m missing here, or a different way to accomplish the same?  I was getting strange results when i did it the “easy” way too, with

    {if count == 5 || count == 10 ..etc }

    I could probably get that way to work, but the “%” php method is so much more elegant.

    Thoughts?

  • #2 / Aug 27, 2009 4:46pm

    Ingmar

    29245 posts

    You only want to display entries where work_status=“Available”, correct? If so, why not simply use

    {exp:weblog:entries weblog="titus_works" search:work_status="=Available" disable="..."

    Also, moving to Howto.

  • #3 / Aug 27, 2009 4:55pm

    ender

    1644 posts

    not to mention that you could avoid using php entirely and just use a {switch} tag to output something on every 5th row.

  • #4 / Aug 27, 2009 5:28pm

    octavianmh

    69 posts

    Ah hah!  Working like a charm — I hadn’t had to use the “search” parameter yet, it all makes sense. 

    Thanks!

  • #5 / Aug 27, 2009 5:29pm

    Ingmar

    29245 posts

    Glad to hear it 😊 Please don’t hesitate to post again as needed.

  • #6 / Aug 27, 2009 7:08pm

    Lisa Wess

    20502 posts

    I just happened on this thread and I was hoping to explain what was going on here.  Let’s take your pared down code:

    {exp:weblog:entries weblog="titus_works" sort="asc"}
    
        {if work_status == "Available"}
            <td valign="top"><a href="http://{url_title_path=" target="_self" rel="noopener">{work_thumbnail}</a></td>
        {/if}
    {/exp:weblog:entries}

    Nevermind the counter for now.  What happens here is that ExpressionEngine first pulls your entries based on your parameters.  In this case that is going to be the oldest (sort=“asc”) 100 entries entries in the titus_works weblog.

    Now it has those entries.  Because of your conditional it is only going to show those that have the work_status of available.  Now, your counter is actually going to act on the total output based on the parameters, and simply hide the ones based on your conditional.

    switch= would have the same issue.

    The reason the search: parameter works is because it is pulling the entries in an already limited fashion based on what that parameter says - so you’ll only ever get entries with an Available status, so the switch or counter is consistent.

    Anyhow, I wanted you to understand this fully so it makes sense. Glad you have a solution!

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

ExpressionEngine News!

#eecms, #events, #releases