- Posts: 9
COMMUNITY FORUM
extrafields do not alternate css class [solved]
- Yanick
- Topic Author
- Offline
- New Member
Less
More
12 years 2 months ago - 12 years 2 months ago #100588
by Yanick
extrafields do not alternate css class [solved] was created 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:
As you can see, there is a mechanism to exclude empty fields and it's this:
But for some reason it does not apply to:
Does anyone know a solution for this problem? I'm not skilled enough in php to find out why exactly this doesn't work.
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
- Topic Author
- Offline
- New Member
Less
More
- Posts: 9
12 years 2 months ago - 12 years 2 months ago #100589
by Yanick
Replied by Yanick on topic Re: itemExtrafields does not alternate css class?
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 commentShould probably work in other templates too.
$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 -->
<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.