内嵌PHP数组并在自己的DIV中显示文本字符串

时间:2015-02-10 19:10:38

标签: php html mysql joomla

我使用以下数据库查询和PHP代码来检索复选框值并将其作为HTML显示在页面上。

$submissionId = JRequest::getInt('submissionId'); 

$db = JFactory::getDbo();
$db->setQuery("SELECT `FormId`, `FieldName`, `FieldValue` FROM `#__rsform_submission_values` WHERE `FormId` = '5' AND `SubmissionId` = '$submissionId' AND `FieldName` = 'documents'");
$rows = $db->loadObjectList();

foreach($rows as $row) {
    $documentsArray[] = $row->FieldValue;
}

$document = implode(' ', $documentsArray);

$formLayout .= '<div class="formContainer">';
$formLayout .= '<div class="formView">';

for($i = 0; $i < count($document); $i++) {
    $formLayout .= "<div>" . $document . "</div>";
}

$formLayout .= '</div>';
$formLayout .= '</div>';

代码部分正常工作。问题出在&#34; FieldValue&#34;包含一个或多个复选框值的行。我需要将每个复选框值格式化并显示在其自己的DIV中,就像我在下面详述的那样。

<div>Checkbox value #1</div>
<div>Checkbox value #2</div>
<div>Checkbox value #3</div>

我只需要显示复选框值而不是复选框本身。有关如何从内爆阵列中选择每个单独字符串并将其显示在其自己的DIV中的任何提示。谢谢。

1 个答案:

答案 0 :(得分:0)

只需删除代码的一小部分:

$submissionId = JRequest::getInt('submissionId'); 

$db = JFactory::getDbo();
$db->setQuery("SELECT `FormId`, `FieldName`, `FieldValue` FROM `#__rsform_submission_values` WHERE `FormId` = '5' AND `SubmissionId` = '$submissionId' AND `FieldName` = 'documents'");
$rows = $db->loadObjectList();

$formLayout .= '<div class="formContainer">';
$formLayout .= '<div class="formView">';

foreach($rows as $row) {
    $formLayout .= "<div>" . $row->FieldValue . "</div>";
}

$formLayout .= '</div>';
$formLayout .= '</div>';

或内爆&#34; divs&#34;:

...
foreach($rows as $row) {
    $documentsArray[] = $row->FieldValue;
}

$document = implode('</div><div>', $documentsArray);

$formLayout .= '<div class="formContainer">';
$formLayout .= '<div class="formView">';
$formLayout .= '<div>' . $document . '</div>';
$formLayout .= '</div>';
$formLayout .= '</div>';