Keyword

if else on extrafield

  • John-Eilif Eliassen
  • John-Eilif Eliassen's Avatar Topic Author
  • Offline
  • Senior Member
More
8 years 3 months ago #155959 by John-Eilif Eliassen
if else on extrafield was created by John-Eilif Eliassen
Hello, i am trying to achieve a certain thing with the extrafields in content module. If one spesific extrafield has a value, it displays something (including the extrafield->value). But if there are no value, it displays something else. But as soon as i throw in a else statement the sites blanks out. The extra-field ID is 1.

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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 3 months ago #155962 by Krikor Boghossian
Replied by Krikor Boghossian on topic if else on extrafield
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; ?>

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
  • John-Eilif Eliassen's Avatar Topic Author
  • Offline
  • Senior Member
More
8 years 3 months ago #155964 by John-Eilif Eliassen
Replied by John-Eilif Eliassen on topic if else on extrafield

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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 3 months ago #155965 by Krikor Boghossian
Replied by Krikor Boghossian on topic if else on extrafield
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

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
  • John-Eilif Eliassen's Avatar Topic Author
  • Offline
  • Senior Member
More
8 years 3 months ago #155966 by John-Eilif Eliassen
Replied by John-Eilif Eliassen on topic if else on extrafield

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
  • John-Eilif Eliassen's Avatar Topic Author
  • Offline
  • Senior Member
More
8 years 3 months ago #155970 by John-Eilif Eliassen
Replied by John-Eilif Eliassen on topic if else on extrafield
When i do this i get the content IF the field has value, but if its empty i get nothing.. hmmm
<?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
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
8 years 2 months ago - 8 years 2 months ago #155977 by Krikor Boghossian
Replied by Krikor Boghossian on topic if else on extrafield
The HTML has some issues,
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 :)
Last edit: 8 years 2 months ago by Krikor Boghossian.

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

  • John-Eilif Eliassen
  • John-Eilif Eliassen's Avatar Topic Author
  • Offline
  • Senior Member
More
8 years 2 months ago #155992 by John-Eilif Eliassen
Replied by John-Eilif Eliassen on topic if else on extrafield

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
  • John-Eilif Eliassen's Avatar Topic Author
  • Offline
  • Senior Member
More
8 years 2 months ago #155994 by John-Eilif Eliassen
Replied by John-Eilif Eliassen on topic if else on extrafield
I tried this to force the else-action.. but it stil displays only the <li class="value"> if the extra field has any value
<?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
  • John-Eilif Eliassen's Avatar Topic Author
  • Offline
  • Senior Member
More
8 years 2 months ago #156010 by John-Eilif Eliassen
Replied by John-Eilif Eliassen on topic if else on extrafield
I tried to use the if(!empty($extraField)) option to rule at that it excludes fields that are empty but might be true in value.. but it still only displays A when there is value in the specified field.. B is never displayed. For the love of god i cant figure this out :D I tried going into all the articles and saving them again since the extrafield was created after the articles, but that did not change anything. I also tried adding a value to a couple of other articles, which displays correctly..
<?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.


Powered by Kunena Forum