1

I have two grid inputs and I need to have a conditional statement that says: if all fields of both grids are equal to 0, output some text. The way I'm trying to do this is by changing a variable value based on an EE if.

<?php $var = 0; ?>
{ex_product_dimensions_g}
  {if ex_product_dimensions_g:height != "0" || ex_product_dimensions_g:width != "0" || ex_product_dimensions_g:depth != "0"}
    {ex_product_dimensions_g:height}(h) x {ex_product_dimensions_g:width}(w) x {ex_product_dimensions_g:depth}(d)<br>
  {if:else}
    <?php $var++; ?>
  {/if}
{/ex_product_dimensions_g}

{product_dimensions_case_g}
  {if product_dimensions_case_g:height != "0" || product_dimensions_case_g:width != "0" || product_dimensions_case_g:depth != "0"}
    {product_dimensions_case_g:height}(h) x {product_dimensions_case_g:width}(w) x {product_dimensions_case_g:depth}(d) (inc. Flightcase)<br>
  {if:else}
    <?php $var++; ?>
  {/if}
{/product_dimensions_case_g}

<?php 
if ($var >= 2):
  echo 'Not specified';
endif;
?>

For some reason though $var seems to always increment regardless of the content in the grid input. Can the EE if statements be used for what I am trying to achieve, or is there a better way to do this?

1 Answer 1

1

You're going to run in to parse order issues here I think. Unfortunately the code is not executing in the order you've written it, so certain evaluations won't happen when you're expecting them to.

There's a certain point when adding PHP in an EE template that it's best to roll out a custom plugin instead. My general rule when doing something like you're doing is to keep EEs template code as separate from PHP code as possible.

Assuming you have PHP set to run after the template, retrieve all your values first in to PHP vars then add PHP logic at the end of the template.

In this case see if increment $var as you have it but do the logic in PHP later. So instead of

{if ex_product_dimensions_g:height != "0" || ex_product_dimensions_g:width != "0" || ex_product_dimensions_g:depth != "0"}
  {ex_product_dimensions_g:height}(h) x {ex_product_dimensions_g:width}(w) x {ex_product_dimensions_g:depth}(d)<br>
{if:else}
  <?php $var++; ?>
{/if}

use:

<?php $var_h = $var_h + '{ex_product_dimensions_g:height}';
      $var_w = $var_w + '{ex_product_dimensions_g:width}';?>

at the end of the template:

<?php if($var_h > 0 || $var_w > 0) echo 'A dimension is zero.'; ?>

Alternatively it might be easier to go over to using P&T's Matrix as that has some built in logic/maths: http://docs.pixelandtonic.com/matrix/templating/field.html#average-lowest-highest-and-sum

1
  • That's spot on! Thank you for sorting that out for me. I took the first approach although I have used Matrix for another project and it's definitely worth it. Feb 3, 2014 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.