Keyword

extrafields do not alternate css class [solved]

  • Yanick
  • Yanick's Avatar Topic Author
  • Offline
  • New Member
More
12 years 2 months ago - 12 years 2 months ago #100588 by Yanick
I just submitted this as a bug but I'd like to find a solution:

I created several extra fields, I filled them out, I left one field blank. When viewing an item, the css should alternate in classes 'odd' and 'even' and skip empty fields. This does not happen. As expected the fields don't show, but the odd-even alternator does not change, resulting in unwanted template output.

Example expected: odd - even - odd [EMPTY FIELD SO NO OUTPUT] - odd [ODD AGAIN] - even - odd - even

Example actual outcome: odd - even - odd [EMPTY FIELD SO NO OUTPUT] - even [SHOULD HAVE BEEN ODD!] - even - odd - even

The following code comes from the default item.php template:
<div class="itemExtraFields">
<?php echo JText::_('K2_ADDITIONAL_INFO'); ?></h3>
        <ul>
        <?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
                <?php if($extraField->value): ?>
                        <li class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
                        <span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
                        <span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
                        </li>
                <?php endif; ?>
                <?php endforeach; ?>
        </ul>
<div class="clr"></div>
</div>

As you can see, there is a mechanism to exclude empty fields and it's this:
<?php if($extraField->value): ?>

But for some reason it does not apply to:
<?php echo ($key%2) ? "odd" : "even"; ?>

Does anyone know a solution for this problem? I'm not skilled enough in php to find out why exactly this doesn't work.

Please Log in or Create an account to join the conversation.

  • Yanick
  • Yanick's Avatar Topic Author
  • Offline
  • New Member
More
12 years 2 months ago - 12 years 2 months ago #100589 by Yanick
Found a solution myself.
$key should not have been reused, that's what's causing all the trouble.
Instead I introduced $count to keep track of it all.
Here is the solution, copy and replace from <ul> to </ul> beneath the comment
<!-- Item extra fields -->
Should probably work in other templates too.
<ul>
        <?php $count = true; ?>
        <?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
        <?php if($extraField->value): ?>
                <li class="<?php echo (($count = !$count)?'odd':'even'); ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
                        <span class="itemExtraFieldsLabel"><?php echo $extraField->name; ?>:</span>
                        <span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
                </li>
        <?php endif; ?>
        <?php endforeach; ?>
</ul>

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum