0

I have an issue with setting up variables - if the custom field is filled then it should be shown, if it's empty - then date (+1 month from today's date) is shown.

However, it doesn't work properly. Custom field values are shown as they should, but I can't get the date if the field value is empty.

<?php

if (isset($fields['exp-date'])) {
    echo $field_value = $fields['exp-date']->rawvalue;
    $timeset = $fields['exp-date']; // then assign
}
if (!isset($fields['exp-date'])) {
    echo (new DateTime('+1 month'))->format('d.m.Y');
    $timeset = '';
}
?>

P.S. I get custom fields shown in module using the next code and FieldsHelper (everything works perfectly).

<?php
$item->jcfields = FieldsHelper::getFields('com_content.article', $item, true);
$fields = [];
foreach($item->jcfields as $jcfield)
{
    $fields[$jcfield->name] = $jcfield;
}
?>
5
  • Hi Azur - overall, I can not make much sense from the code you have posted here. Please give some more context and explain what it works, what it doesn't, what it is supposed it should happen etc. However, you say you get nothing when your custom field is empty, so you would expect that your second if statement should run. But there you are checking if the variable isset ... Have you made sure that this var isn't set, so your code block will run?
    – FFrewin
    Commented Jul 30, 2020 at 9:18
  • Sorry, it's my first time asking on forums for code issues) as usual I try to google the explanation but this case took almost 2 days to solve. Also I'm not that good at PHP so I can't really 100% tell you what the provided code is going to do although I can explain what needs to be done. So there is a div on my website which shows the expiration time value. There should be two ways to get it, each one excludes another one. 1st - when the expiration time sets buy custom field (show the text value) and 2nd - if the custom field is empty then the time is showing in d.m.Y format + 1 month.
    – Azur
    Commented Jul 30, 2020 at 10:00
  • Hey, no worries - we all have been beginners at some point. Regarding your issue - as I said on my first comment, but you can also see in Irata's answer below, the problem should be in the way you check for the variable and what you check it against. Most likely the variable is set anyway (although it can be empty). So you will have to change what you check. I can't comment for the overall code though, (I mean it doesn't look pretty anyway) but what we see is partial and with no much meaningful context, so...
    – FFrewin
    Commented Jul 30, 2020 at 10:05
  • @Azur your question would have been much clearer if you would have shown us what var_dump($fields['exp-date']); displayed. Does your code actually need to echo those values, or does it merely need to declare $timeset? Commented Jul 30, 2020 at 11:25
  • @Azur please mark this page as resolved by awarding the green tick to the answer that successfully and educationally resolves your question (the answer that you select may be your own). Commented Sep 1, 2020 at 9:02

2 Answers 2

0

There is a difference between !isset and empty in PHP so while your code is looking at isset vs !isset but your description is saying empty or not empty.

Your field 'exp-date' can exist but have no value in it meaning it is empty.

Your second statement could be changed to check if the field isset and empty

if (isset($fields['exp-date']) && empty($fields['exp-date'])) {
    echo (new DateTime('+1 month'))->format('d.m.Y');
    $timeset = '';
}

You might find this explanation useful , particularly the table that shows the different states, https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

1
  • Regarding if (isset($variable) && empty($variable) {. This will only evaluate as true if $variable is set, not null, yet is a falsey value (like 0). The same evaluation can be done with if (isset($variable) && !$variable) { Commented Jul 30, 2020 at 11:12
0

Thanks @Irata, your code didn't work for me, but the clues and the provided link gave me a hint.

This is my new code which works as required (although I'm not sure why it works and how I got it):

$field_value = $fields['exp-date']->rawvalue;
$timeset = $field_value;

if (!empty($timeset))
{
    echo $field_value = $fields['exp-date']->rawvalue;
}
else
{
    echo (new DateTime('+1 month'))->format('d.m.Y');
}
3
  • There is still some unnecessary syntax in here. There is no need to declare $field_value, since you later declare $timeset anyhow. Just use $timeset = $fields['exp-date']->rawvalue; Then, assuming that $timeset is always set, you can use if ($timeset) { to check that it is "truthy". There is no need to add $field_value = after the echo because that variable is already declared in the first line of your snippet. The entire condition block can be replaced with echo $timeset ?: (new DateTime('+1 month'))->format('d.m.Y'); Or if you get a Notice, change ?: to ??. Commented Jul 30, 2020 at 11:34
  • Relevant reading: PHP ternary operator vs null coalescing operator Commented Jul 30, 2020 at 11:36
  • Thank you @mickmackusa!
    – Azur
    Commented Jul 30, 2020 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.