Page 1 of 1

Show custom Joomla fields only if they contain data

Posted: Sat Mar 30, 2019 5:03 pm
by delonen
Hi everyone, sorry for my bad english.

I need a helping hand for a problem regarding the display of the new custom fields available in Joomla! (3.9) in the override of the default.php file. (I'm not a php programmer and this Joomla! website it's for a friend).

Thanks so much to anyone who wants to help me out, Regards.


INTRODUCION

- This is the com_content for the article (the relative default.php file at the override path SiteName / templates / FirstNameTemplate / html / com_content / article / default.php)

- I have already entered the code to have the custom fields recognized in the default.php file, it works because with single custom fields I can visualize them and manage them

- The code I'm trying uses the if control structure. Perhaps for semantics in some cases it might be "cleaner" to use a for loop, however for now I would prefer this solution (if structure)



WHAT I WANT TO DO

1) I would like the html code to be printed ONLY if one of the two values (stard-date or end-date, or both, of course) are inserted.

2) If there is no value entered NO html code must be entered (and therefore not even the container "general-container" with <h2>, <p> and <dl>. ANYTHING!

3) If one of the two values (start-date or end-date) is entered, the div must be printed container, <h2>, <p> the <dl> BUT ONLY THE PAIR <dt> <dd> of the corresponding value entered (and not also the other empty).


THE PROBLEM IS IN POINT 3: I can't find a way to get the effect of point 3 :(

----------------------------------------------

THE CODE

This code contains the internal control structures to make all the container div, <h2>, <p> and <dt> <dd> pairs are displayed only if their value is entered.

DOES NOT WORK, in the sense that it does not return error but prints the general container div, <h2> and <p> always (even if no data is entered) and does not show data if entered (whether it is entered one data or both, never show them!) :(

I also tried to delete the lines

$params->get('start-date') &&
$params->get('end-date') &&

but don't works :(



<?php

$showStartDate = isset($myCustomFields['start-date']) and !empty($myCustomFields['start-date']);
$showEndDate = isset($myCustomFields['end-date']) and !empty($myCustomFields['end-date']);

$showDates = $showStartDate || $showEndDate;
if($showDates){ ?>

<div id="contenitore-generale">
<h2>TITOLO DEL BOX</h2>
<p>testo descrittivo..</p>

<dl>
<?php if ($params->get('start-date') && $showStartDate) : ?>
<dt>START DATE</dt>
<dd>
<?php echo $myCustomFields['start-date']; ?>
</dd>
<?php endif; ?>

<?php if ($params->get('end-date') && $showEndDate) : ?>
<dt>END DATE</dt>
<dd>
<?php echo $myCustomFields['end-date']; ?>
</dd>
<?php endif; ?>

</dl>

</div>

<?php } ?>

Re: Show custom Joomla fields only if they contain data

Posted: Mon Apr 08, 2019 10:36 am
by CDXXonline
Can't you start your code with a null case? Leave the first set of brackets empty.

if ($fieldvalue == null) { } else {

.................

}