PHP测试和条件

时间:2017-07-10 14:43:14

标签: php

使用此代码,我试图显示一个具有开始和结束日期的事件日期,但有时只有一个开始日期。

如果有开始和结束日期,我需要显示:   DU 10 JUILLET 2017 AU 10 JUILLET 2017

如果只有开始日期,我需要显示:   LE 10 JUILLET 2017 (“DU”替换为“LE”)

<?php if (isset($this->item->jcfields[3]) && !empty($this->item-jcfields[3])): 
?>Du
<?php echo FieldsHelper::render('com_content.article', 'field.render', 
array('field'=> $this->item->jcfields[3])); ?>
        <?php endif; ?>
        <?php if (isset($this->item->jcfields[7]) && !trim($this->item-
>jcfields[7])): ?>Au
<?php echo FieldsHelper::render('com_content.article', 'field.render', 
array('field'=> $this->item->jcfields[7])); ?>
        <?php endif; ?>

1 个答案:

答案 0 :(得分:1)

未经测试,我尝试在改进格式化的同时保持代码相同。

<?php
if (isset($this->item->jcfields[3]) && !empty($this->item->jcfields[3]) && $this->item->jcfields[3]->value != ''):
    if (isset($this->item->jcfields[7]) && !empty($this->item->jcfields[7]) && $this->item->jcfields[7]->value != ''):
        echo "Du ";
    else:
        echo "Le ";
    endif;
    echo FieldsHelper::render('com_content.article', 'field.render', array('field'=> $this->item->jcfields[3]));
    if (isset($this->item->jcfields[7]) && !empty($this->item->jcfields[7]) && $this->item->jcfields[7]->value != ''):
        echo " Au ";
        echo FieldsHelper::render('com_content.article', 'field.render', array('field'=> $this->item->jcfields[7]));
    endif;
endif;
相关问题