使用PHP在单个行上显示SQL列值


Display SQL columns values on individual rows using PHP

我使用以下MySQL/Joomla数据库查询来返回表中的结果。

$db    = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('FormId', 'FieldName', 'FieldValue')));
$query->from($db->quoteName('#__rsform_submission_values'));
$query->where($db->quoteName('FormId') . ' = ' . $db->quote('5'));
$query->where($db->quoteName('SubmissionId') . ' = ' . $db->quote('4'));
$query->where($db->quoteName('FieldName') . ' = ' . $db->quote('documents'));
$db->setQuery($query);
$rows = $db->loadObjectList();

存储在文档"FieldName"列中的数据是复选框值。如何从列中选择每个单独的复选框值,并使用PHP和HTML将其显示在自己的行中?目前,我正在使用以下PHP/HTML来显示结果。

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

非常感谢您的帮助。非常感谢。

假设您正在努力确保所有字段都有一个要显示的结果,那么您可以在运行查询后尝试以下方法:

//create an array that contains all the fields you want to ensure are  
// displayed (in the order you want them to appear) and set the default
// values to FALSE
$results=array_fill_keys(
    array('documents',
        'pictures',
        'another_field',
        'something_else'
         ), false);
//update any values that were found in database
foreach($rows as $row) {
     $results[$row->FieldName]=$row->FieldValue;   
}
//generate the html
foreach($results as $fieldName=>$fieldValue) {
    $formLayout .= sprintf('<div>The `%s` box was %s</div>',$fieldName,$fieldValue?'checked':'not checked');
}