- Posts: 40
COMMUNITY FORUM
if else on extrafield
- John-Eilif Eliassen
- Topic Author
- Offline
- Senior Member
The code i've tried to push through:
<?php if($params->get('itemExtraFields') && count($item->extra_fields)): ?>
<div class="veilActions"><ul>
<?php foreach($extrafields as $key=>$value): ?>
<?php if($extrafields[$key]->id == '1'&&$extrafields[$key]->value!=''): ?>
<a href="mailto:?subject=<?php echo $item->title; ?>&body=TEXT%0D%0A<?php echo $extrafields[$key]->value; ?>%0D%0ATEXT" class="hasTooltip" data-placement="top" title="TOOLTIP TITLE">
<li class="action"><i class="fa fa-toggle-on" aria-hidden="true"></i>
</li></a>
<? } else { ?>
<a href="mailto:?subject=<?php echo $item->title; ?>&body=TEXT%0D%0A<?php echo $item->introtext; ?>%0D%0ATEXT" class="hasTooltip" data-placement="top" title="TOOLTIP TITLE">
<li class="action"><i class="fa fa-toggle-off" aria-hidden="true"></i></li></a>
<?php endif; ?>
<?php endforeach; ?></ul>
</div>
<?php endif; ?>
Update: This code does not error out the page but nothing is on display. Not if the extrafield has value or not.
Sincerely would appreciate any comments on this as it is crucial that i get this to work.
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
- Posts: 15920
You can use this code as a starting point.
<?php foreach ($item->extra_fields as $extraField): ?>
<?php if($extraField->value != ''): ?>
#do something
<?php else: ?>
#do something else
<?php endif; ?>
<?php endforeach; ?>
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- John-Eilif Eliassen
- Topic Author
- Offline
- Senior Member
- Posts: 40
Krikor wrote: Hey there,
You can use this code as a starting point.
<?php foreach ($item->extra_fields as $extraField): ?> <?php if($extraField->value != ''): ?> #do something <?php else: ?> #do something else <?php endif; ?> <?php endforeach; ?>
Hi Krikor. How do i use this if($extraField->value) for a spesific field?
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
- Posts: 15920
You can also insert a $key in the foreach statement
$item->extra_fields as $extraField=>$key
and checkt the extrafield's position in the array
$item->extra_fields[$key]
An alternative would be to render this extrafield on its own.
github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L104-L109
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- John-Eilif Eliassen
- Topic Author
- Offline
- Senior Member
- Posts: 40
Krikor wrote: You can check the $extraField->alias or $extraField->id matches your criteria and check that specific field's value.
You can also insert a $key in the foreach statement
$item->extra_fields as $extraField=>$key
and checkt the extrafield's position in the array
$item->extra_fields[$key]
An alternative would be to render this extrafield on its own.
github.com/kricore/Advanced-templating-with-K2/blob/master/_inc/cheatsheet.php#L104-L109
I am truly sorry, but that is to deep for my understanding of php and k2. Could you please give me an exact example for the situation below? I have read that cheatsheet page and tried my best several times to populate a logical output that renders my wish. To no luck.. at best i render the page without errors with nothing displaying (where the extrafields should pop up).
<? php if( isset( $this->item->extraFields->Link->value ) && ( $this->item->extraFields->Link->value !== '') ) : ?>
<li>OUTPUT A</li>
<?php else: ?>
<li>OUTPUT B </li>
<?php endif; ?>
Please Log in or Create an account to join the conversation.
- John-Eilif Eliassen
- Topic Author
- Offline
- Senior Member
- Posts: 40
<?php if($params->get('itemExtraFields') && count($item->extra_fields)): ?>
<div class="veilActions">
<ul>
<?php foreach ($item->extra_fields as $extraField): ?>
<?php if($extraField->value != ''): ?>
<!-- IF LINK -->
<a href="#" class="hasTooltip" data-placement="left" title="Send"><li class="action"><i class="fa fa-toggle-on" aria-hidden="true"></i></li></a>
<?php else: ?>
<!-- IF LINK IS MISSING -->
<a href="#" class="hasTooltip" data-placement="top" title="Send"><li class="action"><i class="fa fa-toggle-off" aria-hidden="true"></i></li></a>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
Please Log in or Create an account to join the conversation.
- Krikor Boghossian
- Offline
- Platinum Member
- Posts: 15920
Inside a <ul> or an <ol> you should always start with a <li> element.
<?php if($params->get('itemExtraFields') && count($item->extra_fields)): ?>
<div class="veilActions">
<ul>
<?php foreach ($item->extra_fields as $extraField): ?>
<?php if($extraField->value != '' ): ?>
<!-- IF LINK -->
<li class="action">
<a href="#" class="hasTooltip" data-placement="left" title="Send"><i class="fa fa-toggle-on" aria-hidden="true"></i></a>
</li>
<?php else: ?>
<!-- IF LINK IS MISSING -->
<li class="action">
<a href="#" class="hasTooltip" data-placement="top" title="Send"><i class="fa fa-toggle-off" aria-hidden="true"></i></a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
You can use these snippets to check whether this is a specific field:
<?php if($extraField->alias == 'CHECK_FOR_ALIAS'): ?>
<?php if($extraField->id == 12): ?>
Now apart from that, the code seems correct, is there a link I can see?
JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- John-Eilif Eliassen
- Topic Author
- Offline
- Senior Member
- Posts: 40
Krikor wrote: The HTML has some issues,
Inside a <ul> or an <ol> you should always start with a <li> element.
<?php endif; ?>
You can use these snippets to check whether this is a specific field:
<?php if($extraField->alias == 'CHECK_FOR_ALIAS'): ?> <?php if($extraField->id == 12): ?>
Now apart from that, the code seems correct, is there a link I can see?
I am afraid there are not. It is on local server without option to go live in any shape or form im afraid. But i can show you any output available.
What i did now was try your inputs and put
<?php foreach ($item->extra_fields as $extraField): ?>
<?php if($extraField->value != ''): ?>
<?php if($extraField->id == 1): ?>
<li class="action"><a href="#" class="hasTooltip" title="Send"><i class="fa fa-toggle-on" aria-hidden="true"></i></a></li>
<?php else: ?>
<li class="action"><a href="#" class="hasTooltip" title="Send"><i class="fa fa-toggle-off" aria-hidden="true"></i></a></li>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
But I must be doing something wrong.. since the <li> after phpelse does not show if field->id 1 has no value.
Here you can see that the first one (which is the only one that HAS value in this extrafield, is infact showing the icon/content it should.
imgur.com/HuEgHsV
But here you can see that the list elements that where the extrafield has no value, is not displayed at all.. seems like the whole extrafields area is removed if the field has no value.. is there a setting backend i have missed?
imgur.com/pSbaZUl
Thank you for your brilliant help so far
Please Log in or Create an account to join the conversation.
- John-Eilif Eliassen
- Topic Author
- Offline
- Senior Member
- Posts: 40
<?php foreach ($item->extra_fields as $extraField): ?>
<?php if($extraField->value != '')
{ echo
'<li class="value"><a href="#" class="hasTooltip" title="Send"><i class="fa fa-toggle-on" aria-hidden="true"></i></a></li>';
} else {
echo
'<li class="novalue"><a href="#" class="hasTooltip" title="Send"><i class="fa fa-toggle-off"></i></a></li>';
}
?>
<?php endforeach; ?>
Please Log in or Create an account to join the conversation.
- John-Eilif Eliassen
- Topic Author
- Offline
- Senior Member
- Posts: 40
<?php if($params->get('itemExtraFields')): ?>
<div class="veilActions">
<ul>
<?php foreach ($item->extra_fields as $extraField): ?>
<?php $ks = $extraField->id == 1; ?>
<?php if(!empty($ks)): ?>
<li class="action value">
A
</li>
<?php else: ?>
<li class="action novalue">
B
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
imgur.com/AtScbAe
Please Log in or Create an account to join the conversation.