Mat5hias Mat5hias - 3 months ago 11
PHP Question

PHP If state - Render HTML only if there is text output from Joomla module text field int the backend

I try to build a simple FAQ Module in Joomla 3.

I have 4 fields in the module backend. "Question 1 & Answer 1" "Question 2 & Answer 2".

My php and Html is build like this:

<div id="accordion">
<h3 class="question"> <?php echo $params->get('question1'); ?></h3>
<div class="question">
<?php echo $params->get('answer1'); ?>
</div>

<h3 class="question"> <?php echo $params->get('question2'); ?></h3>
<div class="question">
<?php echo $params->get('answer2'); ?>
</div>
</div>


I like to add some php with a if statement so the code block only renders if the textfields in the backend if they are filled out from the user:

(If the field "question3" have any text, show this code)

<?php if ($this->params->get('question3')) : ?> //This is the line of code i do not know how to do.
<h3 class="question"> <?php echo $params->get('question2'); ?></h3>
<div class="question">
<?php echo $params->get('answer2'); ?>
</div>
</div>

<?php endif; ?>


This is how the XML looks like:

<!-- Question 1 -->
<field
label="Question 1"
default=""
name="question1"
description="Fill in the question"
type="text"
size="60"
/>
<!-- End of Question 1 -->

<!-- Answer 1 -->
<field
label="Answer1"
default="Fill in the answer"
name="answer1"
description="Fill in the answer"
type="editor"
width="200"
height="100"
hide="readmore,pagebreak"
filter="safehtml"
size="260"
/>
<!-- End of Answer 1 -->

<field type="spacer" name="questionspacer2" label="&lt;b&gt;Question and Answer 2 1&lt;/b&gt;" />


<!-- Question 2 -->
<field
label="Question 2"
default=""
name="question2"
description="Fill in the question"
type="text"
size="60"
/>
<!-- End of Question 2 -->

<!-- Answer 2 -->
<field
label="Answer2"
default=""
name="answer2"
description="Fill in the answer"
type="editor"
width="200"
height="100"
hide="readmore,pagebreak"
filter="safehtml"
size="260"
/>
<!-- End of Answer 2 -->


Any hint would be great! I am already looking all night for a solution.

Answer

Depending on how you set the parameters if not filled you need to check the output like this:

$answer2 = $params->get('answer2'));

// (Applies if the not filled data returns NULL etc.)
if ( isset($answer2) ) { YOUR CODE } 

// (Applies if the not filled data returns an empty string)
if ( !empty($answer2) ) { YOUR CODE } 

not tested, but it should work ;)

EDIT: Just made this how it should look like, just as discussed in the comments below :)