开发者

How to use PHP variables in my ExpressionEngine template for counting?

开发者 https://www.devze.com 2023-04-13 01:27 出处:网络
Hokay, so. Here\'s my embedded template, chilling: <? $i=0; ?> {exp:channel:entries channel=\"products\"

Hokay, so. Here's my embedded template, chilling:

<? $i=0; ?>

{exp:channel:entries 
    channel="products" 
    dynamic="no" 
    entry_id="{embed:ids}"
}

    <? $i++; ?>

    {exp:playa:parents 
        field_id="25" 
        limit="1"
    }

        <!-- product -->

        {if no_parents}
            <? $i--; ?>
        {/if}

    {/exp:playa:parents}

    {if no_results}
        No results!
    {/if}

{/exp:channel:entries}

<? if ($i === 0 ) { echo 'No products found!'; } ?>

The logic I had for this $i variable was to get an accurate reading of whether any results have been output. "Result" in this sense refers to what gets output by Playa.

The exp_channel_entries's no_results test only gets triggered if {embed:ids} is empty or the embedded ids don't correspond to entries in the channel. If the entries method returns entries but none of the results have a parent entry, nothing gets output -- and I needed a way to determine this, and I thought "Hmm, PHP should be able to do this, right?"

The desired outcome is that 'No products found!' gets output when $i = 0 but for some reason, $i is always 0 regardless of what entries get spit out.

Oh, and before you ask: YES, PHP is indeed enabled. Example: Below, {embed:ids} = 41|78|79|80|81|87|106. When set to OUTPUT, the PHP tags just get printed in the source:

<? $i=0; ?>
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<!-- product -->
<? $i++; ?>
<? $i--; ?>
<? $i++; ?>
<!-- product -->
<? if ($i === 0 ) { echo 'No products found!'; } ?>

If I switch PHP parsing to INPUT the tags get processed, but $i = 0 every time.

I added an echo $i; after $i=0, $i++, and $i--开发者_如何学JAVA. With PHP set to OUTPUT, as before, the statements just get output in the page source. With PHP set to INPUT, I get this string of values: 0 1 1 1 1 1 01

So my questions to you, StackOverflow community, is:

1) Why does PHP in OUTPUT mode just output the PHP tags without processing them?

2) How can I keep count of the number of product parents being output?


I couldn't tell you why your PHP isn't being parsed when switched to output (never seen that before), but I do think there's a simpler way to do this:

{exp:query sql="SELECT child_entry_id FROM exp_playa_relationships WHERE parent_field_id = 25 AND child_entry_id IN({embed:ids})"}
    {exp:playa:parents field_id="25" entry_id="{child_entry_id}" limit="1"}
    <!-- product -->
    {/exp:playa:parents}
    {if no_results}<p>No products found!</p>{/if}
{/exp:query}

This query will only return IDs of entries which do indeed have parents. The only thing you'll have to do is change your passed ids embed from using pipes to commas.

You could also try this:

{exp:channel:entries channel="products" entry_id="0{exp:query sql="SELECT parent_entry_id FROM exp_playa_relationships WHERE parent_field_id = 25 AND child_entry_id IN({embed:ids})"}|{parent_entry_id}{/exp:query}" dynamic="no"}
    <!-- product -->
    {if no_results}<p>No products found!</p>{/if}
{/exp:channel:entries}


I realize that this thread is old but I'm posting just in case someone else has similar problems and can't follow Derek's awesome solution due to architectural issues.

My issues were solved with -

  1. Adding full php start tags (with php)
  2. Remove all braces and going with the alternative syntax

    foreach(items as item):
    
        ...
    
    endforeach;
    
  3. Changing permissions on the file to 755.

  4. Changing the group owner of the file to the default web user

  5. Deleting all template manager entries (if that doesn't work, just edit the file on template manager itself)

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消