I'm using a plugin that allows for multiple extra field groups to be assigned to a K2 category - the plugin works fine, the issue I'm having is being able to pull the data from a certain Extra Field Group, rather than looping through all groups.
This is the code I have (that was listed on the developer site):
<?php
$extraFieldsByGroup = JArrayHelper::pivot($this->item->extra_fields, 'group');
$db = JFactory::getDbo();
$query = "SELECT * FROM #__k2_extra_fields_groups";
$db->setQuery($query);
$groups = $db->loadAssocList('id');
foreach ($extraFieldsByGroup as $key=>$extrafields){
$defaultGroup = '';
if(!is_array($extrafields)) $extrafields = array($extrafields);
if($key == $this->item->category->extraFieldsGroup)
$defaultGroup = "class='mefg_default'";
?>
<div id="group" class="<?php echo $groups[$key]['name'];?>" >
<div class="<?php echo $groups[$key]['name'];?>">
<div>
<h3><?php echo JText::_($groups[$key]['name']); ?></h3>
</div>
<ul>
<?php foreach ($extrafields as $key=>$extraField): ?>
<?php if($extraField->value != ''): ?>
<?php if($extraField->type == 'header'): ?>
<h4 class="itemExtraFieldsHeader"><?php echo $extraField->name; ?></h4>
<?php else: ?>
<div>
<span class="itemExtraFieldsLabel"><?php echo JText::_($extraField->name); ?>:</span>
<span class="itemExtraFieldsValue <?php echo $extraField->alias; ?>"><?php echo $extraField->value; ?></span></div>
<?php endif; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div></div>
<?php
}
?>
<div class="clr"></div>
What I'd like to do, is pull the extra fields from a certain group and display them only (the values) in a loop.
There "are" certain ways to do it such as
<?php echo $item->extraFields->EXTRA_FIELD_ALIAS_HERE->value; ?>
but some of the fields will be blank or have null values, so creating a bunch of empty divs isn't really an option for me.
The $extraFieldsByGroup variable is pulling from all "groups", and the one I'm looking for is "2". My php skills are limited, but if anyone could point me in the right direction to create a loop for the extra field values inside group "2", it would be very appreciated. Thank you!
K2's Extra fields are stored in a json format - and that is possibly the reason why you are having hard time retrieving them. See: http://www.itoctopus.com/on-leveraging-the-power-of-k2s-extra-items
Also, if you provide more context to what needs to be done exactly, I'm sure I'll be able to answer your question much more thoroughly.