[英]PHP If state - Render HTML only if there is text output from Joomla module text field int the backend
我嘗試在Joomla 3中構建一個簡單的FAQ模塊。
我在模塊后端有4個字段。 “問題1和答案1”“問題2和答案2”。
我的php和HTML的構建方式如下:
<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>
我喜歡用if語句添加一些php,這樣,如果用戶填寫了文本字段,則代碼塊僅在后端的文本字段出現時才呈現:
(如果“ question3”字段中有任何文本,請顯示此代碼)
<?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; ?>
XML如下所示:
<!-- 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="<b>Question and Answer 2 1</b>" />
<!-- 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 -->
任何提示都會很棒! 我已經整夜都在尋找解決方案。
根據未設置參數時如何設置參數,您需要檢查輸出,如下所示:
$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 }
未經測試,但它應該可以工作;)
編輯:只是使它看起來應該像下面的評論中討論的那樣:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.